Qt / C ++中多继承的正确方法是什么? [英] What is the correct way of Multiple inheritance in Qt/C++?

查看:1924
本文介绍了Qt / C ++中多继承的正确方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的Qt应用程序,我有一个基类如下。我使用QObject,因为我想使用Signal-Slot机制在所有派生类。

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();
}

然后我有另一个类如下。我需要从QWidget扩展,因为我需要在所有派生类中实现事件处理方法(mouseMoveEvent(),keyPressEvent()..等);

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.(mouseMoveEvent(),keyPressEvent()..etc);

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


推荐答案

在当前的化身中,不可能在派生类的多个继承路径中使用 QObject (像您的 IRzLayeringPlugin class)。我见过的唯一解决方案是创建一个没有任何 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.

这篇关于Qt / C ++中多继承的正确方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆