我如何从 QWidget 和 QThread 继承?

发布于 2024-08-27 16:32:12 字数 179 浏览 5 评论 0原文

我有一个像这样的类

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

滥情空心 2024-09-03 16:32:15

您不能从多个 QObject 继承。

您可以继承其中一个并将另一个设为成员变量并从那里开始工作。

class GUI : public QWidget 
{
  QThread myThread;
};

您已命名您的类 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.

class GUI : public QWidget 
{
  QThread myThread;
};

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.

爱已欠费 2024-09-03 16:32:14

你不能。 QWidgetQThread 都(非虚拟地)继承自 QObject。因此,您没有虚拟派生,因此没有 QObject 的两个副本,这会使编译器感到困惑。 QObject 就是专门这样设计的。请参阅:

据称有些人绕过了这个(现在找不到链接,但它在 Google 上,我有两周前同样的麻烦),但充其量是不安全的。

编辑:最好的方法可能是让另一个对象继承 QThread 并将该对象保留为 GUI 类中的成员。这是大多数人在这件事上采取的解决方法。

You can't. Both QWidget and QThread inherit (non-virtually) from QObject. You therefore do not have virtual derivation, thus two copies of QObject, 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.

请叫√我孤独 2024-09-03 16:32:14

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 the moveToThread() function of a widget.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文