Qt/C++ 中多重继承的正确方法是什么?
在我的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在当前版本中,无法在派生类(例如
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 yourIRzLayeringPlugin
class). The only solution I've ever seen was to create an interface class without anyQObject
inheritence, but with functions that directly correspond to theQObject
functions you want to use, then implement the bridge between the interface and your otherQObject
class inheritance in your specific class. It gets ugly fairly quickly.今天这里有一个类似的问题。
基本上,需要做两件事:
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:
Q_DECLARE_INTERFACE
after the interface class declarationQ_INTERFACES
macro of the classAfter 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.