獨立線程中實現QT GUI

在網上搜集的資料:python

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

據這些資料看,在非主線程中處理QT或多或少都會有些問題,但我在文檔裏卻是沒有明確找到說QApplication必定要定義在主線程中。app

因此下面嘗試了一下:oop

 1 #include <windows.h>
 2 #include <process.h>    /* _beginthread, _endthread */
 3 #include <stdlib.h>
 4 #include <QtGui/QApplication>
 5 #include <QtGui/QPushButton>
 6 
 7 void initUI(void* dummy) {
 8     int argc = 0;
 9     QApplication app(argc, NULL);
10     QPushButton button("Hello, world");
11     button.show();
12     app.exec();
13     printf("gui thread dead.\n");
14     _endthread();
15 }
16 
17 int main(int argc, char** argv)
18 {
19     /* Launch gui thread. */
20     _beginthread( initUI, 0, NULL );
21 
22     /* Do something with main thread */
23     while(1){
24         printf("tick ...\n");
25         
26         Sleep(1000);
27     }
28 }

運行結果來看彷佛並無多少問題:測試

後續再看。ui

----------------附:PYQT編譯步驟----------------spa

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.net

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.zipcode

4. 在VS2010的命令提示行環境下執行(編譯sip並安裝pyd至python目錄):

1 cd c:/sip-4.15.4
2 python configure.py
3 nmake
4 nmake install

5. 在VS2010的命令提示行環境下執行(編譯pyqt):

1 cd c:/pyqt-win-gpl-4.10.3
2 python configure-np.py
3 nmake
4 nmake install

6. 在python解釋器中測試:

>>> import sipconfig         #測試sip
>>> from PyQt4.QT import *   #測試pyqt
相關文章
相關標籤/搜索