我想在單獨的線程中執行一些工作,並在工作完成時停止該線程。QThread不會停止/不處理信號
我已經建立了這樣
thread.connect(workerClass, SIGNAL(finished()), SLOT(quit()));
但連接的退出()當我發射完成的()信號時隙不會被調用。命令行不顯示與連接失敗相關的任何警告。
我創建了一個簡單的測試項目,以縮小問題,我也得到相同的行爲:
TestClass.h:
#ifndef TESTCLASS_H
#define TESTCLASS_H
class TestClass : public QObject
{
Q_OBJECT
public:
TestClass() { }
signals:
void finished();
public slots:
void doWork();
}
#endif // TESTCLASS_H
TestClass.cpp:
#include "TestClass.h"
#include <QtCore/QDebug>
void TestClass::doWork()
{
qDebug() << "Starting";
for(long i = 0; i < 1000000000; i++);
qDebug() << "Done";
emit finished();
};
和main.cpp:
#include <QtCore/QCoreApplication>
#include <QtCore/QThread>
#include "TestClass.h"
int main(int argc, char* argv[])
{
QCoreApplication a(argc, argv);
TestClass testClass;
QThread testThread;
testClass.moveToThread(&testThread);
testThread.connect(&testClass, SIGNAL(finished()), SLOT(quit()));
testClass.connect(&testThread, SIGNAL(started()), SLOT(doWork()));
testThread.start();
testThread.wait();
return 0;
}
所以這是我期望發生:
- 一旦
testThread.start()
得到處理,線程的run()
函數被調用,它在內部調用exec()
,並開始一個事件循環。另外它會拋出一個started()
信號。 testClass
已將其doWork()
插槽調用。- 當
TestClass::doWork()
完成時,它會拋出一個finished()
信號。 - 的
testThread
事件循環從等待作爲該線程已結束接收該信號並且將其轉發給其quit()
槽 testThread.wait()
回報。
步驟1-3工作,我得到qDebug()
輸出。 問題在於步驟4:信號被觸發並轉發到某個事件循環(我調試了一下,但無法弄清楚它被觸發了哪個事件循環),但從不調用quit()
插槽。
我在做什麼錯?
P.S .:請不要建議子類化QThread來代替,我嘗試這種方法的全部觀點是因爲我想避免子類化QThread。
是的,我認爲你是對的。他應該'int ret = a.exec(); testThread.wait();返回ret;'。 – RedX
默認情況下線程有自己的事件循環,所以我認爲這就足夠了。事實證明,如果主事件循環沒有運行,它們不值一提。你們是對的。 –
如果您在其中調用int QThread :: exec(),QThread只會有一個事件循環運行。事件循環基本上是一個處理while循環中的東西的隊列。沒有處理事件和信號的循環=>沒有信號到達。 –