Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it safe to use *virtual* multiple inheritance if QObject is being derived from DIRECTLY?

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?

like image 624
Dan Nissenbaum Avatar asked Oct 04 '22 04:10

Dan Nissenbaum


1 Answers

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.

like image 50
Dan Milburn Avatar answered Oct 13 '22 10:10

Dan Milburn