Qt/C++ 中多重继承的正确方法是什么?

发布于 2024-09-08 18:30:03 字数 1029 浏览 4 评论 0原文

在我的 Qt 应用程序中,我有一个基类,如下所示。我使用 QObject 是因为我想在所有派生类中使用信号槽机制。

class IRzPlugin : public QObject {

public:
  virtual void registerMenu(QWidget*);
  virtual void execute();
}

然后我还有另一堂课,如下。我需要从 QWidget 扩展,因为我需要在所有派生类中实现事件处理方法(例如 mouseMoveEvent()keyPressEvent() 和其他的)。

class IRzLayeringPlugin : public IRzPlugin , public QWidget{

}

但编译器给出了这些错误:

C:\svn\osaka3d\tags\iter08\prototype\osaka3d\rinzo\plugin\moc_IRzLayeringPlugin.cxx: In member function `virtual const QMetaObject* IRzLayeringPlugin::metaObject() const':
C:\svn\osaka3d\tags\iter08\prototype\osaka3d\rinzo\plugin\moc_IRzLayeringPlugin.cxx:51: error: `QObject' is an ambiguous base of `IRzLayeringPlugin'
C:\svn\osaka3d\tags\iter08\prototype\osaka3d\rinzo\plugin\moc_IRzLayeringPlugin.cxx:51: error: `QObject' is an ambiguous base of `IRzLayeringPlugin'
make[2]: *** [CMakeFiles/Rinzo.dir/plugin/moc_IRzLayeringPlugin.cxx.obj] Error 1

In my Qt application, I have a base class as follow. I am using QObject because I want to use Signal-Slot mechanism in all derived classes.

class IRzPlugin : public QObject {

public:
  virtual void registerMenu(QWidget*);
  virtual void execute();
}

Then I have another class as follow. I need to extend from QWidget because I need to implement event handling methods in all derived classes (such as mouseMoveEvent(), keyPressEvent() and others).

class IRzLayeringPlugin : public IRzPlugin , public QWidget{

}

But compiler gives these the errors:

C:\svn\osaka3d\tags\iter08\prototype\osaka3d\rinzo\plugin\moc_IRzLayeringPlugin.cxx: In member function `virtual const QMetaObject* IRzLayeringPlugin::metaObject() const':
C:\svn\osaka3d\tags\iter08\prototype\osaka3d\rinzo\plugin\moc_IRzLayeringPlugin.cxx:51: error: `QObject' is an ambiguous base of `IRzLayeringPlugin'
C:\svn\osaka3d\tags\iter08\prototype\osaka3d\rinzo\plugin\moc_IRzLayeringPlugin.cxx:51: error: `QObject' is an ambiguous base of `IRzLayeringPlugin'
make[2]: *** [CMakeFiles/Rinzo.dir/plugin/moc_IRzLayeringPlugin.cxx.obj] Error 1

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

苍暮颜 2024-09-15 18:30:03

在当前版本中,无法在派生类(例如 IRzLayeringPlugin 类)的多个继承路径中使用 QObject。我见过的唯一解决方案是创建一个没有任何 QObject 继承的接口类,但具有直接对应于您要使用的 QObject 函数的函数,然后实现接口与特定类中的其他 QObject 类继承之间的桥梁。它很快就会变得丑陋。

In the current incarnation, it isn't possible to use QObject in multiple inheritance paths for a derived class (like your IRzLayeringPlugin class). The only solution I've ever seen was to create an interface class without any QObject inheritence, but with functions that directly correspond to the QObject functions you want to use, then implement the bridge between the interface and your other QObject class inheritance in your specific class. It gets ugly fairly quickly.

泡沫很甜 2024-09-15 18:30:03

今天这里有一个类似的问题。

基本上,需要做两件事:

  • 在接口类声明之后添加 Q_DECLARE_INTERFACE
  • 将接口添加到类的 Q_INTERFACES 宏中

此后,qobject_cast 将使用您的接口。

如果您想使用接口中的信号和槽,那么您就不走运了,因为您只能使用从 QObject 派生的类型来执行此操作。但在这种情况下,您总是会收到 'QObject' is a ambigeous base of 'IRzLayeringPlugin' 错误。

在这种情况下,@Caleb 的想法仍然是最好的。

There was a similar question today here.

Basically, two things are needed:

  • Adding Q_DECLARE_INTERFACE after the interface class declaration
  • Adding the interface to the Q_INTERFACES macro of the class

After this, qobject_cast will work with your interfaces.

If you'd like to use signals and slots from the interfaces, you are out of luck, because you can only do that with types that derive from QObject. But in this case, you would always get the 'QObject' is an ambiguous base of 'IRzLayeringPlugin' error.

In this case, @Caleb's idea is still the best.

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