非主线程中的QApplication [英] QApplication In Non-Main Thread

查看:407
本文介绍了非主线程中的QApplication的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要exec()一个QApplication在不是主要的线程(我的GUI必须是可以在运行时动态加载和卸载的插件,所以我没有访问主线程)。有没有人知道一个(相对)无痛的方式来攻击Qt的限制以外的主要的启动QApplication?



我正在使用gcc4在C ++中使用C ++开发Qt4。 3.4。

解决方案

如果你正在使用QThread,那么你已经有正常的Qt事件循环,只需运行exec ()函数。虽然您无法使用主线程之外的GUI对象,但您仍然可以通过排队的信号/插槽连接与它们交互。也许你可以尝试存储指向主线程QThread对象的指针,并调用QObject :: moveToThread()将你的GUI对象移动到主线程,而不是把QApplication移动到另一个线程。



我认为尝试使用不同类型的hacks和kluges来防范工具包并不是个好主意。


I need to exec() a QApplication in a thread that is not main (my GUIs must be plugins that can be dynamically loaded and unloaded at runtime, so I have no access to the main thread). Does anyone know of a (relatively) painless way to hack around Qt's restriction against starting QApplication outside of main?

I'm developing in Linux with Qt4 in C++ using gcc4.3.4.

解决方案

If you are using QThread then you already have normal Qt event loop and can just run exec() inside QThread::run() function. While you can't work with GUI objects outside of the main thread you still can interact with them through queued signal/slot connections. Maybe you can try to store pointer to the main thread QThread object and call QObject::moveToThread() to move your GUI objects to the main thread instead of moving QApplication into another thread.

I think it's not really good idea to try to go against toolkit with different kind of hacks and kluges.

这篇关于非主线程中的QApplication的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆