5

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?

4

1 回答 1

4

QObject不, Qt 不支持多重继承。

问题不在于虚拟继承,而在于 Qt 的元对象系统。每个QObject基类都有一个关联QMetaObject的管理信号、槽、属性等,并且每个元对象都知道它QObject的父类,因此可以处理存在于父类中的信号。Qt moc 无法处理来自QObject其子类或其任何子类的多重继承。

于 2013-05-08T17:06:19.840 回答