我想在线程完成运行时通知对象。但是,我无法让线程正常退出。我有以下代码:QThread - 使用插槽quit()退出线程
Processor.cpp
thread = new QThread;
tw = new ThreadWorker;
connect(tw, SIGNAL(updateStatus(QString)), this, SLOT(statusUpdate(QString)));
tw->doSetup(thread, strDic);
tw->moveToThread(thread);
thread->start();
while(thread->isRunning())
{
}
qDebug() << "Thread Finished";
ThreadWorker.cpp
void ThreadWorker::doSetup(QThread *thread, const string &path)
{
_strPath = path;
connect(thread, SIGNAL(started()), this, SLOT(run()));
connect(this, SIGNAL(finished()), thread, SLOT(quit())); //tried terminate() also
}
void ThreadWorker::run()
{
DirectorySearch dicSearch;
vector<string> vecFileList = dicSearch.getFileList(_strPath);
emit updateStatus("Directory Fetched");
emit finished();
}
的退出()槽似乎并没有停止线程(的QThread :: isFinished从未返回true )。有人能指引我朝着正确的方向吗?
(注:ThreadWorker不从QThread的继承)
只是为了测试...尝试在while循环中添加QCoreApplication :: instance() - > processEvents()。这只是一个建议,所以我不会将它作为答案发布。这可能是因为QThread本身位于主线程中,所以它的OWN槽可能在主事件循环中调用,而不是ThreadWorker的槽,它应该在QThread中执行。 – Raiv 2011-04-26 16:35:34
@Raiv:spot-on,我认为 – 2011-04-26 17:09:08