我如何从 QWidget 和 QThread 继承?
我有一个像这样的类
class GUI : public QWidget, public QThread
当我执行上述操作时,我收到有关连接信号的错误。 错误显示对“connect”的引用不明确
。有没有办法同时继承两者?
谢谢
I have a class like this
class GUI : public QWidget, public QThread
When I do the above i get errors about connect signals. The error says Reference to "connect" is ambiguous
. Is there a way to inherit from both?
Thank you
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您不能从多个 QObject 继承。
您可以继承其中一个并将另一个设为成员变量并从那里开始工作。
您已命名您的类 GUI - 这是您程序的主 GUI 吗?请参阅 Qt 示例文件夹中的示例 - 他们在 GUI 上都有示例程序 和线程。
You cannot inherit from multiple QObjects.
You can inherit from one and make the other a member variable and work from there.
You've named your class GUI - is this the main GUI of your program? See the examples in the Qt examples folder - they have sample programs on both GUI's and Threads.
你不能。
QWidget
和QThread
都(非虚拟地)继承自QObject
。因此,您没有虚拟派生,因此没有 QObject 的两个副本,这会使编译器感到困惑。QObject
就是专门这样设计的。请参阅:据称有些人绕过了这个(现在找不到链接,但它在 Google 上,我有两周前同样的麻烦),但充其量是不安全的。
编辑:最好的方法可能是让另一个对象继承 QThread 并将该对象保留为 GUI 类中的成员。这是大多数人在这件事上采取的解决方法。
You can't. Both
QWidget
andQThread
inherit (non-virtually) fromQObject
. You therefore do not have virtual derivation, thus two copies ofQObject
, which confuses the compiler.QObject
was specifically designed this way. See:There are some who allegedly went around this (can't find the link right now, but it's out there on Google, I had the same trouble two weeks ago), but it is unsafe at best.
Edit: the best way would probably be to have another object inherit from QThread and keep that object as a member in your
GUI
class. That is the sort of workaround most people do in this matter.QWidgets
和 GUI 相关对象不能位于与应用程序主线程不同的线程中。您不应该同时继承它们。您还不能调用小部件的moveToThread()
函数。QWidgets
and GUI related objects con not be in different threads than the application main thread. You should not inherit both of them. You can not also call themoveToThread()
function of a widget.