C++ QObject多重继承

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8578657/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 18:49:15  来源:igfitidea点击:

QObject Multiple Inheritance

c++qtuser-interface

提问by Anonymous

I am trying to use mix in classes for C++/Qt to provide a whole bunch of widgets with a common interface. The interface is defined in such as way so that if it is defined as the base class for other widget classes, then the widget themselves will have those signals:

我试图在 C++/Qt 的类中使用 mix 来提供一大堆具有通用接口的小部件。接口以这样的方式定义,如果它被定义为其他小部件类的基类,那么小部件本身将具有这些信号:

class SignalInterface: public QObject {
    Q_OBJECT

    public:
    SignalInterface();
    virtual ~SignalInterface();

    signals:
    void iconChanged(QIcon);
    void titleChanged(QString);
}

class Widget1: public SignalInterface, QWidget{

    public:
    Widget1()
    virtual ~Widget1()

    // The Widget Should Inherit the signals
}

Looking at the class hierarchy the problem becomes apparent, I have stumbled on to the dreaded diamond in multiple inheritance, where the Widget1inherits from QWidgetand SignalInterface, and both which inherit from QObject. Will this cause any issues?

查看类层次结构,问题变得很明显,我偶然发现了多重继承中可怕的菱形,其中Widget1继承自QWidgetSignalInterface,而两者都继承自QObject. 这会导致任何问题吗?

We know that this problem can be easily solved if the QObjectclass is pure virtual (which is not the case).

我们知道,如果QObject类是纯虚拟的(事实并非如此),这个问题很容易解决。

A possible solution would be:

一个可能的解决方案是:

class Interface: public QWidget {
Q_OBJECT

signals:
void IconChanged(QIcon);
void titleChanged(QString);
}

class Widget1: public Interface {

}

The issue here is that I already have lot of code that inherit from QWidget, and its painful to hack that in. Is there another way?

这里的问题是我已经有很多继承自 的代码QWidget,并且很难破解它。还有其他方法吗?

回答by szatmary

Unfortunately inheriting QObjecttwice will cause problems in moc.

不幸的是,继承QObject两次会导致moc.

From http://qt-project.org:

来自http://qt-project.org

If you are using multiple inheritance, moc assumes that the first inherited class is a subclass of QObject. Also, be sure that only the first inherited class is a QObject.

如果您使用多重继承,则moc 假定第一个继承的类是 QObject 的子类。另外,请确保只有第一个继承的类是 QObject

I would suggest using something more like the delegate pattern, or recreate with a HasA not a IsA relationship.

我建议使用更像委托模式的东西,或者使用 HasA 而不是 IsA 关系重新创建。

回答by Carlton

Qt allows multiple inheritance if the base class inherits privatelyfrom QObject.

如果基类从 QObject私有继承,Qt 允许多重继承。

Example:

例子:

class Base: private QObject {
   Q_OBJECT
   /*Can use signals and slots like any other QObject-derived class*/
};

class Derived1: public Base {
   /*Cannot use signals/slots because it does not "see" that Base inherits from QObject*/
};

class Derived2: public QWidget, public Base {
   Q_OBJECT
   /*Can use signals/slots plus has all the functionality of QWidget and Base*/
};

Of course, private inheritance is a different animal altogether and may not give you the solution you really need. What I use it for is when I can get away with using signals/slots only in the base class. When I really do need QObjectbehavior in a derived class, I inherit from QObjectspecifically for just that class.

当然,私有继承完全是另一种动物,可能无法为您提供真正需要的解决方案。我使用它的目的是当我只能在基类中使用信号/插槽时。当我确实需要QObject派生类中的行为时,我QObject专门为该类继承。