如何在QThread上处理事件
How do I process events on a QThread?
通常,如果我在处理密集型函数中,我可以调用QCoreApplication::processEvents()
或QEventLoop::processEvents()
,以确保我的处理不会阻塞其他信号和插槽。
但是,如果我创建一个新的QThread
并将一个工作线程移动到该线程,那么我就没有QCoreApplication
或QEventLoop
可以用来调用processEvents()
。
根据我的研究,我似乎应该能够在我创建的新QThread
上安装QEventLoop
,然后我可以在该QEventLoop
上调用processEvents()
。
然而,我不知道该怎么做。我想它可能看起来像这样:
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
我试图开始循环的每一个地方都有某种问题。但是,即使这样的东西起作用,我该如何在QEventLoop()
上调用processEvents()
?
或者,QThread
也有一个函数setEventDispatcher()
,QAbstractEventDispatcher
有一个processEvents()
函数,但我似乎找不到任何QAbstractEventDispatcher
的子类。
在QThread
上的强化工作人员功能期间,处理事件的正确方法是什么?
根据文档,调用QCoreApplication::processEvents()
会处理调用它的线程的事件。
但是,如果我创建一个新的
QThread
并将一个工作线程移动到该线程,那么我就没有QCoreApplication
或QEventLoop
可以用来调用processEvents()
。
没错-不要叫它。你不需要它,你不想要它。
相关文章:
- 从文本文件中读取时钟时间和事件时间并进行处理
- 处理闪烁窗口事件
- 给定顺序中的事件处理
- C++事件系统 - 多态事件和事件处理程序
- 是否可以在QT GUI应用程序中处理事件时播放加载动画指示器?
- 如果窗口服务被杀,如何处理事件
- GRPC C 称为PloteimionQueue关闭,但PostemionQueue.Next在有待处理事件时永远阻
- WNDPROC处理事件更少的代码
- C FLTK 1.3.2:最新的屏幕,处理事件和非障碍睡眠
- 在运行线程中处理事件
- 如何在Windows C++中处理事件
- 使用 EVT_NAVIGATION_KEY 处理事件不起作用
- C++ 从另一个程序集创建 WPF 窗口的父级并处理事件
- 在QT OpenGL中处理事件
- 如何在QThread上处理事件
- 移动精灵和处理事件
- 如何在QThread中正确处理来自长时间运行的任务的信号和事件
- 如何处理事件消息,同时使用chrome嵌入式框架(CEF)
- 处理事件从wxTextCtrl在wxFrame - c++ /wxWidgets
- Qt如何处理事件发出的无限循环?