問題2:窗口操作只能運(yùn)行于主線程(可能原因?yàn)榇翱谛枰猀Application的支持,而QApplication是定義在主線程中的,窗口運(yùn)行在主事件循環(huán)中,而次線程的exec只是屬于它的次線程循環(huán))。當(dāng)在次線程中QMessageBox();錯(cuò)誤如下: ASSERT failure in QWidget: "Widgets must be created in the GUI thread.", file kernel/qwidget.cpp, line 1069 QObject::killTimers: timers cannot be stopped from another thread。我想要在此線程做了某個(gè)操作后就彈出個(gè)對話框進(jìn)行提示,那該怎么辦。、
答案:既然主線程負(fù)責(zé)窗口操作,那么我們就把這個(gè)事交給主線程來做。我是在次線程中emit一個(gè)信號,而這個(gè)信號連接的槽運(yùn)行在主線程中,所以讓這個(gè)槽來QMessageBox;這也說明了,如果一個(gè)功能在一個(gè)地方運(yùn)行不了,那么我們可以通過信號槽機(jī)制使得它在別的地方運(yùn)行,不能運(yùn)行的地方拋信號,能運(yùn)行的地方定義槽,信號和槽進(jìn)行合適的連接,一般在構(gòu)照函數(shù)中(對象的開始函數(shù)哦)。在help文檔中有一句The main event loop receives events from the window system and dispatches these to the application widgets. Generally speaking, no user interaction can take place before calling exec(). 也就是說一切的窗口操作事件都是由a.exec分配的,分配到各個(gè)子窗口部件中的。