简体   繁体   English

QThread: slot quit() 永远不会被调用,因此线程 wait() 是永远的

[英]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.

相关问题 QThread-使用插槽quit()退出线程 - QThread - Using a slot quit() to exit the thread QThread :: quit()会立即结束线程还是等到返回事件循环? - Does QThread::quit() immediately end the thread or does it wait until returning to the event loop? QThread :: quit是否可以从正在运行的线程中使用 - Is QThread::quit usabled from within the running thread QThread 线程间通信:连接到 &amp;QThread::quit 与连接到 lambda [&amp;thread] { thread-&gt;quit();} 的奇怪行为 - QThread interthread communication: strange behavior connecting to &QThread::quit vs connecting to a lambda [&thread] { thread->quit();} QThread::exit 或 QThread::quit 不会终止由 derived class 创建的线程 - QThread::exit or QThread::quit won't terminate the thread created by derived class QThread :: wait()和QThread :: finished() - QThread::wait() and QThread::finished() 如何使用 QWaitCondition 实现一个永远运行的 QThread{},但在执行此操作时仍需要捕获另一个 Slot - How to implement a QThread that runs forever{} with a QWaitCondition but still needs to catch another Slot while doing that QObject:没有这样的插槽QThread :: readyRead() - QObject: no such slot QThread::readyRead() 使主线程等到所有其他Qthread完成 - Making the main thread wait till all other Qthread finished 在Qt中,当事件循环的线程拥有的QObject上的一个槽正在执行时,QThread的事件循环是否阻塞? - In Qt, does a QThread's event loop block while a slot on a QObject owned by the event loop's thread is executing?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM