如何为QWidget继承的类创建线程?
我想为 QWidget 继承的类创建一个线程。实际上,我尝试使用 QThread 进行多重继承,但它失败了,我想使用线程运行特定的成员函数。我怎样才能实现这个目标?有人知道吗?
I want to create a thread for a class which is Inherited by QWidget. Actually, I tried with multiple inheritance with QThread and it fails and I want to run particular member function using thread. How can I achieve this? Does anyone have any idea?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以使用实现线程并调用小部件方法的包装类:
但是,您不应该在“threadMethod”中调用任何 QWidget 方法,因为 GUI 以及小部件属于“主”线程,并且 QWidget 方法不是线程安全的!
最好将您的小部件和线程代码完全分开。
You could use a wrapper class that implements the thread and calls your widget's method:
However, you should not call any QWidget methods in "threadMethod", since the GUI and and thus the widgets belong to the "main" thread, and the QWidget methods are not thread-safe!
It would probably better to keep your widget and thread code completely separate.
一种解决方案可能是使用嵌套类,在其中您将传递一个指向普通小部件类的指针以及嵌套运行方法中您需要的所有方法。
One solution could be to use nested class in which you will pass a pointer to your normal widget class and all all methods you need from nested run method.
属于 GUI 模块的 Qt 类是不可重入的。它们必须从主线程运行。
Qt classes which belong to the GUI module are not reentrant. They MUST be run from the main thread.