Qt库事件循环问题
我正在编写一个DLL,它被另一个应用程序用作插件,并希望利用Qt的能力.
我已经设置,编译和运行所有类,但没有发出任何信号. 所以似乎没有QEventLoop. 尝试1: 尝试2: 我不确定该怎么做.我显然无法在将使用我的插件的应用程序中创建QCoreApplication,并且我无法在没有插件的情况下发出信号. 我已经包含了一个简单的(并且可怕写的)测试应用程序,它应该说明我的问题: 任何帮助,将不胜感激! main.cpp中: #include <iostream> #include "ThreadThing.h" using namespace std; int main(int argc,char *argv[]) { cout << "Main: " << 1 << endl; ThreadThing thing1; cout << "Main: " << 2 << endl; thing1.testStart(); cout << "Main: " << 3 << endl; thing1.testEnd(); cout << "Main: " << 4 << endl; thing1.wait(-1); cout << "Main: " << 5 << endl; return 0; } ThreadThing.h: #ifndef THREADTHING_H #define THREADTHING_H #include <QThread> class ThreadThing : public QThread { Q_OBJECT public: ThreadThing(); virtual void run(); void testStart(); void testEnd(); public slots: void testSlot(); signals: void testSignal(); }; #endif//THREADTHING_H ThreadThing.cpp: #include "ThreadThing.h" #include <iostream> #include <QCoreApplication> using namespace std; ThreadThing::ThreadThing() { cout << "Constructor: " << 1 << endl; this->start(); cout << "Constructor: " << 2 << endl; } void ThreadThing::run() { cout << "Run: " << 1 << endl; int i = 0; cout << "Run: " << 2 << endl; QCoreApplication* t = new QCoreApplication(i,0); cout << "Run: " << 3 << endl; connect(this,SIGNAL(testSignal()),this,SLOT(testSlot()),Qt::QueuedConnection); cout << "Run: " << 4 << endl; t->exec(); cout << "Run: " << 5 << endl; } void ThreadThing::testStart() { cout << "TestStart: " << 1 << endl; emit testSignal(); cout << "TestStart: " << 2 << endl; } void ThreadThing::testEnd() { cout << "TestEnd: " << 1 << endl; this->quit(); cout << "TestEnd: " << 1 << endl; } void ThreadThing::testSlot() { cout << "TEST WORKED" << endl; } 输出: Main: 1 Constructor: 1 Constructor: 2 Main: 2 TestStart: 1 TestStart: 2 Main: 3 TestEnd: 1 TestEnd: 1 Main: 4 Run: 1 Run: 2 WARNING: QApplication was not created in the main() thread. Run: 3 Run: 4 解决方法
您必须创建QCoreApplication或QApplication,您必须在主线程中执行此操作.
这并不意味着你不能在你的插件中添加代码…除非应用程序总是在自己的线程中运行每个插件. 如果应用程序正在执行该操作,那么您可以尝试连接到应用程序使用的任何本机事件循环,并安排它在主线程中调用插件中的某些函数. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
- (转)解决windows10下无法安装.net framework 3.5,错误代码
- windows-server-2008-r2 – SAN重建后无法销毁Windows 2008
- windows – 如何使用PowerShell切换当前用户?
- windows-runtime – 在WinRT中更改UI文化
- windows-server-2008 – Server 2008 R2启动时间为2小时.现
- 无法连接到从Windows 7运行Ubuntu的VirtualBox上安装的Redi
- 如何在Windows上使用’rhc’命令进入openshift应用程序?
- Windows server 2008 R2 配置AD域控服务并为用户设置统一桌
- .net – 如何获取和设置通用Windows应用程序的音量级别
- windows-server-2003 – DNS和DHCP不同意IP地址