[英]QThread: slot quit() is never invoked and hence the thread wait() is forever
I have the exact same problem as described QThread won't stop / does not process a signal but with a twist.我有与所描述的完全相同的问题QThread 不会停止/不处理信号但有一个扭曲。 I cannot get it to work even with QCoreApplication::processEvents()
to my while loop.即使将QCoreApplication::processEvents()
用于我的 while 循环,我也无法让它工作。 I followed this blog post and ended up like as follows:我关注了这篇博文,结果如下:
MediaController.cpp媒体控制器.cpp
void MediaController::buttonPlayClicked(bool checked)
{
if (checked)
{
m_loopThread = new QThread;
m_playPauseworker = new PlayPauseWorker;
m_playPauseworker->moveToThread(m_loopThread);
connect(m_playPauseworker, &PlayPauseWorker::dataReady, this, &MediaController::onDataReady);
connect(m_loopThread, &QThread::started, m_playPauseworker, &PlayPauseWorker::process);
connect(m_playPauseworker, &PlayPauseWorker::finished, m_loopThread, &QThread::quit); // <-- never works
connect(m_playPauseworker, &PlayPauseWorker::finished, m_playPauseworker, &PlayPauseWorker::deleteLater);
connect(m_loopThread, &QThread::finished, m_loopThread, &QThread::deleteLater);
m_loopThread->start();
}
else
{
m_loopThread->requestInterruption();
}
}
The above slot is called every time play/pause checkable button is clicked.每次单击播放/暂停可检查按钮时都会调用上述插槽。 Thread and worker are created on the main thread.线程和工作者是在主线程上创建的。
PlayPauseWorker.cpp PlayPauseWorker.cpp
void PlayPauseWorker::process()
{
while (!QThread::currentThread()->isInterruptionRequested())
{
// heavy computations
emit dataReady(std::tuple<QImage, QImage, QImage>>); // <-- works!
QCoreApplication::processEvents(); // <-- doesn't help with quit()
}
emit finished(); // <-- emits the signal but quit() not called
}
On further processing, when i access wait()
to see if my thread has exited, it never returns.在进一步处理中,当我访问wait()
以查看我的线程是否已退出时,它永远不会返回。
In addition to quit()
slot never called, i noticed my GUI becomes very choppy when the thread is running.除了从未调用过的quit()
插槽外,我注意到当线程运行时我的 GUI 变得非常不稳定。 I sometimes can/cannot click on other buttons or play with UI.我有时可以/不能点击其他按钮或玩 UI。 I went through a lot of SO posts but couldn't figure out a way to cleanly exit the thread whenever i need to.我浏览了很多 SO 帖子,但无法找到一种在需要时干净地退出线程的方法。 Not sure where am i going wrong.不知道我哪里错了。
After trying out a lot of suggestions i still couldn't figure out why emit finished()
doesn't quit the thread.在尝试了很多建议之后,我仍然无法弄清楚为什么emit finished()
不会退出线程。 So, as a last resort, i used this->thread->quit()
to achieve the same.因此,作为最后的手段,我使用this->thread->quit()
来实现相同的目的。
But thanks to all the commenters here, i narrowed down on laggy/choppy gui issue.但是感谢这里的所有评论者,我缩小了滞后/不稳定的 gui 问题。 My emit data(std::tuple<QImage, QImage, QImage>>)
was setting pixmap on QGraphicsScene
on a loop without removing the already set pixmap on the scene leading to huge memory leak and ultimately crash.我的emit data(std::tuple<QImage, QImage, QImage>>)
在循环上设置QGraphicsScene
上的像素图,而没有删除场景上已经设置的像素图,导致巨大的 memory 泄漏并最终崩溃。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.