pbootcms网站模板|日韩1区2区|织梦模板||网站源码|日韩1区2区|jquery建站特效-html5模板网

在非 Qt 應(yīng)用程序中使用基于 Qt 的 DLL

Using a Qt-based DLL in a non-Qt application(在非 Qt 應(yīng)用程序中使用基于 Qt 的 DLL)
本文介紹了在非 Qt 應(yīng)用程序中使用基于 Qt 的 DLL的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

問題描述

我做對了嗎?

我的一個客戶有一個小組,我在那里開發(fā)基于 Qt 的客戶端-服務(wù)器內(nèi)容,其中包含許多有趣的小部件和套接字.

A client of mine has a group where I'm developing Qt-based client-server stuff with a lot of fun widget stuff and sockets.

公司內(nèi)的另一個小組想要使用基于 QTcpSocket 的客戶端數(shù)據(jù)提供程序類的包裝版本.(基本上就是它聽起來的樣子,從服務(wù)器向客戶端顯示提供數(shù)據(jù))

Another group within the company wants to use a wrapped version of the QTcpSocket-based client data provider classes. (Which does basically what it sounds like, provides data from the server to the client displays)

但是,該小組有一個主要使用 MFC 構(gòu)建的龐大應(yīng)用程序,而且這種情況在短期內(nèi)不會改變.基于 Qt 的 DLL 也是延遲加載的,因此在某些配置中可以在沒有此功能的情況下部署它.

However, that group has a huge application built mostly with MFC, and that is simply not going to change any time soon. The Qt-based DLL is also delay-loading so that it can be deployed without this feature in certain configurations.

我已經(jīng)讓它工作了,但它有點hacky.這是我目前的解決方案:

I've got it working, but it's a little hacky. Here's my solution at the moment:

DLL 包裝類構(gòu)造函數(shù)調(diào)用 QCoreApplication::instance() 來查看它是否為 NULL.如果它為 NULL,則假定它在非 Qt 應(yīng)用程序中,并創(chuàng)建它自己的 QCoreApplication 實例:

The DLL wrapper class constructor calls QCoreApplication::instance() to see if it's NULL or not. If it's NULL, it assumes it's in a non-Qt app, and creates a QCoreApplication instance of it's own:

if (QCoreApplication::instance() == NULL)
{
    int argc = 1;
    char* argv[] = { "dummy.exe", NULL };
    d->_app = new QCoreApplication(argc, argv);  // safe?
}
else
    d->_app = NULL;

然后它會設(shè)置一個windows定時器來偶爾調(diào)用processEvents():

It then will set up a windows timer to occasionally call processEvents():

if (eventTimerInterval > 0)
{
    // STATE: start a timer to occasionally process the Qt events in the event queue
    SetTimer(NULL, (UINT_PTR)this, eventTimerInterval, CDatabaseLayer_TimerCallback);
}

回調(diào)只是使用 timerID 作為指向類實例的指針來調(diào)用 processEvents() 函數(shù).SetTimer() 文檔說,當(dāng) HWND 為 NULL 時,它會忽略 timerID,因此這似乎是完全有效的.

The callback simply calls the processEvents() function using the timerID as a pointer to the class instance. The SetTimer() docs say when HWND is NULL it ignores the timerID, so this appears to be perfectly valid.

VOID CALLBACK BLAHBLAH_TimerCallback(HWND hwnd, UINT uMsg, UINT_PTR idEvent, DWORD dwTime)
{
    ((BLAHBLAH*)idEvent)->processEvents(); // basically just calls d->_app->processEvents();
}

然后我銷毀 QCoreApplication 實例作為析構(gòu)函數(shù)中的最后一件事.

I then destroy the QCoreApplication instance as the very last thing in the destructor.

BLAHBLAH::~BLAHBLAH()
{
    .. other stuff

   QCoreApplication* app = d->_app;
   d->_app = NULL;
   delete d;
   if (app != NULL)
       delete app;
}

如果托管應(yīng)用程序希望自己對 processEvents() 的調(diào)用計時,它可以為 eventTimerInterval 傳遞 0 并調(diào)用 BLAHBLAH::processEvents() 本身.

If the hosting application wishes to time the calls to processEvents() itself, it can pass 0 in for eventTimerInterval and call BLAHBLAH::processEvents() itself.

對此有什么想法嗎?將該應(yīng)用程序移植到 Qt 不是一種選擇.這不是我們的.

Any thoughts on this? Porting that app to Qt is not an option. It's not ours.

它似乎有效,但這里可能有幾個假設(shè)被打破.我可以用這樣的虛擬參數(shù)構(gòu)造一個 QCoreApplication 嗎?事件隊列以這種方式運行是否安全?

It appears to work, but there are probably several assumptions being broken here. Can I just construct a QCoreApplication with dummy arguments like that? Is the event queue safe to operate in this manner?

我不想以后這件事在我臉上炸開.想法?

I don't want this blowing up in my face later. Thoughts?

推薦答案

研究 Qt 代碼,似乎需要 QCoreApplication 來調(diào)度系統(tǒng)范圍的消息,例如計時器事件.諸如信號/插槽甚至 QThread 之類的東西都不依賴于它,除非它們與那些系統(tǒng)范圍的消息相關(guān).這是我在共享庫中執(zhí)行此操作的方法(以跨平臺方式使用 Qt 本身)并且我實際上確實調(diào)用了 exec,因為 processEvents() 本身并不能處理所有內(nèi)容.

Studying the Qt code it seems QCoreApplication is needed to dispatch system-wide messages such as timer events. Things like signal/slots and even QThreads do not depend on it unless they are related to those system-wide messages. Here is how I do this in a shared library (in a cross platform way using Qt itself) and I actually do call exec, because processEvents() alone does not process everything.

我有一個全局命名空間:

I have a global namespace:

// Private Qt application
namespace QAppPriv
{
    static int argc = 1;
    static char * argv[] = {"sharedlib.app", NULL};
    static QCoreApplication * pApp = NULL;
    static QThread * pThread = NULL;
};

我在 QObject 中有一個 OpenApp 方法(即 moc'ed),如下所示:

I have an OpenApp method in a QObject (that is moc'ed) like this:

// Initialize the app
if (QAppPriv::pThread == NULL)
{
    // Separate thread for application thread
    QAppPriv::pThread = new QThread();
    // Direct connection is mandatory
    connect(QAppPriv::pThread, SIGNAL(started()), this, SLOT(OnExec()), Qt::DirectConnection);
    QAppPriv::pThread->start();
}

這里是 OnExec 插槽:

if (QCoreApplication::instance() == NULL)
{
    QAppPriv::pApp = new QCoreApplication(QAppPriv::argc, QAppPriv::argv);
    QAppPriv::pApp->exec();
    if (QAppPriv::pApp)
        delete QAppPriv::pApp;
}

到目前為止它似乎工作正常,我不確定最后是否需要刪除該應(yīng)用程序,如果我發(fā)現(xiàn)了什么我會更新我的答案.

So far it seems to be working fine, I am not sure if I need to delete the app at the end, I will update my answer if I find something.

這篇關(guān)于在非 Qt 應(yīng)用程序中使用基于 Qt 的 DLL的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請聯(lián)系我們刪除處理,感謝您的支持!

相關(guān)文檔推薦

How to make a .lib file when have a .dll file and a header file(有.dll文件和頭文件時如何制作.lib文件)
Convert std::string to QString(將 std::string 轉(zhuǎn)換為 QString)
Gui toolkits, which should I use?(Gui 工具包,我應(yīng)該使用哪個?)
How to avoid entering library#39;s source files while debugging in Qt Creator with gdb?(使用gdb在Qt Creator中調(diào)試時如何避免進入庫的源文件?)
Starting QTimer In A QThread(在 QThread 中啟動 QTimer)
Making a borderless window with for Qt(使用 for Qt 制作無邊框窗口)
主站蜘蛛池模板: 视频教程导航网_视频教程之家_视频教程大全_最新视频教程分享发布平台 | 浙江筋膜枪-按摩仪厂家-制造商-肩颈按摩仪哪家好-温州市合喜电子科技有限公司 | 京马网,京马建站,网站定制,营销型网站建设,东莞建站,东莞网站建设-首页-京马网 | 杭州月嫂技术培训服务公司-催乳师培训中心报名费用-产后康复师培训机构-杭州优贝姆健康管理有限公司 | 深圳VI设计-画册设计-LOGO设计-包装设计-品牌策划公司-[智睿画册设计公司] | 合肥网带炉_安徽箱式炉_钟罩炉-合肥品炙装备科技有限公司 | 全自动包装秤_全自动上袋机_全自动套袋机_高位码垛机_全自动包装码垛系统生产线-三维汉界机器(山东)股份有限公司 | 专业广州网站建设,微信小程序开发,一物一码和NFC应用开发、物联网、外贸商城、定制系统和APP开发【致茂网络】 | 岩棉板|岩棉复合板|聚氨酯夹芯板|岩棉夹芯板|彩钢夹芯板-江苏恒海钢结构 | 安德建奇火花机-阿奇夏米尔慢走丝|高维|发那科-北京杰森柏汇 | LED灯杆屏_LED广告机_户外LED广告机_智慧灯杆_智慧路灯-太龙智显科技(深圳)有限公司 | 卧涛科技有限公司科技项目申报公司|高新技术企业申报|专利申请 | 铣刨料沥青破碎机-沥青再生料设备-RAP热再生混合料破碎筛分设备 -江苏锡宝重工 | 肉嫩度仪-凝胶测试仪-国产质构仪-气味分析仪-上海保圣实业发展有限公司|总部 | 井式炉-台车式回火炉-丹阳市电炉厂有限公司 | 森旺-A级防火板_石英纤维板_不燃抗菌板装饰板_医疗板 | 酵素生产厂家_酵素OEM_酵素加盟_酵素ODM_酵素原料厂家_厦门益力康 | 继电器模组-IO端子台-plc连接线-省配线模组厂家-世麦德 | 热镀锌槽钢|角钢|工字钢|圆钢|H型钢|扁钢|花纹板-天津千百顺钢铁贸易有限公司 | 大功率金属激光焊接机价格_不锈钢汽车配件|光纤自动激光焊接机设备-东莞市正信激光科技有限公司 定制奶茶纸杯_定制豆浆杯_广东纸杯厂_[绿保佳]一家专业生产纸杯碗的厂家 | 巨野电机维修-水泵维修-巨野县飞宇机电维修有限公司 | 工业车间焊接-整体|集中除尘设备-激光|等离子切割机配套除尘-粉尘烟尘净化治理厂家-山东美蓝环保科技有限公司 | 泰国试管婴儿_泰国第三代试管婴儿_泰国试管婴儿费用/多少钱_孕泰来 | 氟塑料磁力泵-不锈钢离心泵-耐腐蚀化工泵厂家「皖金泵阀」 | 净化车间装修_合肥厂房无尘室设计_合肥工厂洁净工程装修公司-安徽盛世和居装饰 | 山东钢格板|栅格板生产厂家供应商-日照森亿钢格板有限公司 | 智能汉显全自动量热仪_微机全自动胶质层指数测定仪-鹤壁市科达仪器仪表有限公司 | 全自动贴标机-套标机-工业热风机-不干胶贴标机-上海厚冉机械 | SRRC认证_电磁兼容_EMC测试整改_FCC认证_SDOC认证-深圳市环测威检测技术有限公司 | 酶联免疫分析仪-多管旋涡混合仪|混合器-莱普特科学仪器(北京)有限公司 | 儿童乐园|游乐场|淘气堡招商加盟|室内儿童游乐园配套设备|生产厂家|开心哈乐儿童乐园 | 碎石机设备-欧版反击破-欧版颚式破碎机(站)厂家_山东奥凯诺机械 高低温试验箱-模拟高低温试验箱订制-北京普桑达仪器科技有限公司【官网】 | 恒温恒湿试验箱厂家-高低温试验箱维修价格_东莞环仪仪器_东莞环仪仪器 | 千斤顶,液压千斤顶-力良企业,专业的液压千斤顶制造商,shliliang.com | 蒸汽吸附分析仪-进口水分活度仪|康宝百科 | 立刷【微电签pos机】-嘉联支付立刷运营中心 | 大通天成企业资质代办_承装修试电力设施许可证_增值电信业务经营许可证_无人机运营合格证_广播电视节目制作许可证 | 润滑脂-高温润滑脂-轴承润滑脂-食品级润滑油-索科润滑油脂厂家 | 沈阳建筑设计公司_加固改造设计_厂房设计_设计资质加盟【金辉设计】 | 北京办公室装修,办公室设计,写字楼装修-北京金视觉装饰工程公司 北京成考网-北京成人高考网 | 超声波焊接机_超音波熔接机_超声波塑焊机十大品牌_塑料超声波焊接设备厂家 |