通常,如果我处于进程密集型函数中,我可以调用QCoreApplication::processEvents()
或QEventLoop::processEvents()
确保我的处理不会阻塞其他信号和插槽。
但是,如果我创建一个新QThread
线程并将一个工作线程移动到该线程,那么我没有 aQCoreApplication
或 aQEventLoop
可以调用processEvents()
.
根据我的研究,似乎我应该能够在我创建QEventLoop
的新设备上安装一个QThread
,然后我可以调用processEvents()
它QEventLoop
。
但是,我无法弄清楚如何做到这一点。我想它可能看起来像这样:
QThread *thread = new QThread(this);
Worker *worker = new Worker(this);
QEventLoop *loop = new QEventLoop();
connect(thread, SIGNAL(finished()), worker, SLOT(deleteLater()));
connect(thread, SIGNAL(finished()), thread, SLOT(deleteLater()));
connect(thread, SIGNAL(started()), worker, SLOT(startProcessing()));
connect(worker, SIGNAL(done()), thread, SLOT(quit()));
connect(worker, SIGNAL(done()), loop, SLOT(quit()));
worker->moveToThread(thread);
//loop->exec() // blocks processing of this thread
loop->moveToThread(thread);
//loop->exec() // loop is not a member of this thread anymore and even
// if it was, this would block the thread from starting
thread->start();
//loop->exec(); // loop is not a member of this thread anymore and even
// if it was, this would block this thread from continuing
我尝试启动循环的每个地方都有某种问题。但是,即使这样的事情奏效了,我该如何调用processEvents()
呢QEventLoop()
?
或者,QThread
也有一个函数setEventDispatcher()
并且QAbstractEventDispatcher
有一个processEvents()
函数,但我似乎找不到任何子类QAbstractEventDispatcher
。
在 a 上的密集工作函数期间处理事件的正确方法是QThread
什么?