我的原始问题在这里:
Deconstruct object gives QCoreApplication::sendEvent: "Cannot send events to objects owned by a different thread

我见过很多人说,在将QObject移到workerthread后,您无法从gui线程中删除它。

像这样:

//In the GUI thread
QThread* workerThread = new QThread(this);
worker->moveToThread(workerThread);
...
//Still in the Gui thread but somewhere else
delete worker;//Is this wrong?

在这种情况下,如果我想在需要销毁工作线程时停止工作线程,则不是我唯一的选择是执行以下操作:
connect(worerThread,&finished,worker,&deleteLater)?
...
//when I no longer need the worker & the worker thread
workerThread->quit();
workerThread->wait();

最佳答案

您可以并且应该使用

//Still in the Gui thread but somewhere else
worker->deleteLater();

然后在worker中删除workerThread。另外,也不需要停止workerThread

有关 QObject::deleteLater() 的更多信息,请参见Qt文档。

09-06 18:48