在Qt GUI事件线程中检测该"I'm running"
Detect that "I'm running" in Qt GUI event thread
我有这个函数来更新一些GUI的东西:
void SavedConnections::renderList()
{
// Do GUI stuff! Must run in Qt thread!!!
...
}
我需要确保此函数不是从其他线程调用的。我打算做的是将其推迟到事件循环中并发出警告:
void SavedConnections::renderList()
{
if(!this_thread_is_Qt_GUI_thread()) {
qDebug()<< "Warning: GUI operation attempted from non GUI thread!n";
QCoreApplication::postEvent(this, new UpdateGUIEvent());
return;
}
// Do GUI stuff! Must run in Qt thread!!!
...
}
这种模式也非常方便使方法保证在 GUI 线程中异步运行,没有任何丑陋的语法。我已经问过关于Java的ExecutorService的类似问题。
您可以检查当前线程是否是对象所在的线程:
if (QThread::currentThread() != this->thread()) {
// Called from different thread
}
请注意,这可能不是主要的 GUI 线程!它是this
所在的线程(请参阅 QObject 线程相关性)。如果不使用 QObject::moveToThread
更改它,则它是创建对象的线程。
这也是QCoreApplication::postEvent
用来确定事件应该发布到哪个线程中的方法。目标线程必须运行QEventLoop
来响应事件。
因此,如果您的对象不在主 GUI 线程中,请检查主 GUI 线程 ( qApp->thread()
),但发布到this
线程可能不起作用。但是,如果你在那里做GUI的东西,它应该存在于GUI线程中。
相关文章:
- C++GTKMM gui循环依赖关系
- QTcpSocket在不阻塞GUI的情况下重新连接到服务器
- 我的程序有一个保存配置文件的GUI,如何双击此配置文件以直接加载带有配置数据的GUI?
- 在哪里放置我的函数?进入我的母语 Gui 还是进入我的演示者?
- 如何在带有 gdb GUI 前端的 ARM gdbserver 的 PC 上执行远程 gdb 会话?
- 在 Gui Win32 中创建子窗口C++
- 如何防止 std::thread 在 QT 中冻结 GUI?
- 调试控制台 ChessEngine.exe "used"在另一个应用程序(国际象棋 GUI)中
- 更改 Windows GUI 中静态文本框的字体大小
- 我不知道如何使用代码块win32 gui
- Qt GUI使用Qfiledialogbox和qlabel显示视频,这是我的代码
- QtConcurrent - 在发布到 UI 线程的数千个结果中保持 GUI 响应
- 德尔福在回调中出现 GUI 问题
- 从 GUI 文件资源管理器执行程序,并且不生成终端
- 线程和 GUI 处理
- C++ WinAPI[GDI].自定义 gui 正确重绘
- Qt,操作缓慢时不要冻结GUI输入元素
- GCP 或 AWS 是否支持 C++ 和 GUI 应用程序开发?
- 试图打开并读取一个.txt文件,但它从原始文件中删除了实际文本(Qt GUI C++
- 在Qt GUI事件线程中检测该"I'm running"