独立线程中实现QT GUI

时间:2023-12-16 11:47:08

在网上搜集的资料:

http://www.qtcentre.org/threads/16552-Starting-QT-GUI-in-a-seperate-Thread
http://*.com/questions/16812602/qt-main-gui-and-other-thread-events-loops
http://*.com/questions/9777911/how-do-i-create-a-window-in-different-qt-threads
http://*.com/questions/16501284/qt-updating-main-window-with-second-thread

据这些资料看,在非主线程中处理QT或多或少都会有些问题,但我在文档里倒是没有明确找到说QApplication一定要定义在主线程中。

所以下面尝试了一下:

 #include <windows.h>
#include <process.h> /* _beginthread, _endthread */
#include <stdlib.h>
#include <QtGui/QApplication>
#include <QtGui/QPushButton> void initUI(void* dummy) {
int argc = ;
QApplication app(argc, NULL);
QPushButton button("Hello, world");
button.show();
app.exec();
printf("gui thread dead.\n");
_endthread();
} int main(int argc, char** argv)
{
/* Launch gui thread. */
_beginthread( initUI, , NULL ); /* Do something with main thread */
while(){
printf("tick ...\n"); Sleep();
}
}

运行结果来看似乎并没有多少问题:

独立线程中实现QT GUI

后续再看。

----------------附:PYQT编译步骤----------------

1. 下载QT library for windows并安装(C:/QT/4.8.5),并将C:/QT/4.8.5/bin目录添加到系统环境变量中(后续编译依赖qt的qmake):
http://download.qt-project.org/official_releases/qt/4.8/4.8.5/qt-win-opensource-4.8.5-vs2010.exe

2. 下载PYQT source package并解压(C:/PyQt-win-gpl-4.10.3)
http://sourceforge.net/projects/pyqt/files/PyQt4/PyQt-4.10.3/PyQt-win-gpl-4.10.3.zip

3. 下载SIP source package并解压(C:/sip-4.15.4)
http://sourceforge.net/projects/pyqt/files/sip/sip-4.15.4/sip-4.15.4.zip

4. 在VS2010的命令提示行环境下执行(编译sip并安装pyd至python目录):

 cd c:/sip-4.15.
python configure.py
nmake
nmake install

5. 在VS2010的命令提示行环境下执行(编译pyqt):

 cd c:/pyqt-win-gpl-4.10.
python configure-np.py
nmake
nmake install

6. 在python解释器中测试:

>>> import sipconfig         #测试sip
>>> from PyQt4.QT import * #测试pyqt