更新时间:2023-02-26 21:13:51
这是我看到的情况。
主线程你不需要等待工人退出。您的 closeEvent
应该看起来像这样:
In your main thread you do not need to wait for worker to quit. Instead your closeEvent
should look like this:
if (workerThread->isRunning())
{
workerThread->stop();
event->ignore();
}
要使这项工作,你还需要关闭主窗口,完成:
To make this work, you also need to close main window, when worker thread finishes:
connect(workerThread, SIGNAL(finished()), SLOT(close()));
最后,使用 _isFinished
所有其他变量在工作线程)应该发生在工作线程。您可以用一个简单的技巧实现它:
Finally, all manipulations with _isFinished
(and all other variables in worker thread) should occur in worker thread. You can achieve it with a simple trick:
WorkerThread.h
public:
void stop();
protected slots:
void p_stop();
protected:
QMutex mutex;
WorkerThread.cpp
void WorkerThread::stop()
{
staticMetaObject.invokeMethod(this, "p_stop", Qt::QueuedConnection);
}
void WorkerThread::p_stop();
{
QMutexLocker locker(&mutex);
_isFinished = true;
}
这样可以避免死锁。