I understand that in general, multiple inheritance from QObject
-derived classes (even virtual multiple inheritance) is not supported in Qt.
I understand the reason to be (I think) that even in the virtual inheritance case, the Qt classes do not themselves virtually inherit from QObject
. For example, if you attempt to derive a class virtually from both QWidget
and QThread
, this is placing the virtual inheritance in an irrelevant place in the inheritance chain and you still wind up with two QObject
instances.
I therefore think it is safe, and supported in Qt, to use virtual inheritance where the ONLY Qt class being derived from is QObject
itself.
I have:
class Top : public QObject {};
class Left : public virtual Top {};
class Right : public virtual Top {};
class Bottom : public Left, public Right {}; // Is this safe, and supported by Qt?
Note that instances of Bottom
truly have only one instance of Top
(and hence only one instance of QObject
), so it seems that the rationale for avoiding multiple inheritance in Qt (even virtual multiple inheritance) does not apply here.
The above construct nonetheless results in the Qt compiler warning Class Bottom inherits from two QObject subclasses Left and Right. This is not supported!
.
Am I correct? Is it safe to ignore the Qt compiler warning in this specific scenario? Is the above construct, involving virtual multiple inheritance directly from QObject, safe and supported in Qt?
No, multiple inheritance from QObject
is not supported by Qt in any way.
The problem is not with virtual inheritance, it's Qt's meta-object system. Each QObject
base class has an associated QMetaObject
which manages signals, slots, properties, etc, and each meta-object knows its parent QObject
so e.g. signals which exist in parent classes can be handled. The Qt moc is not able to deal with multiple inheritance from QObject
or any of its sub-classes.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With