22.3.2.  Multiple Inheritance with QObject

[ fromfile: multiple-inheritance.xml id: multinheritance-qobject ]

Many classes in Qt use multiple inheritance. Figure 22.3 shows the inheritance relationship between classes used in QGraphicsView. QGraphicsItem is a lightweight object, which does not support signals or slots. QGraphicsObject inherits from QObject first, and QGraphicsItem second, and can offer the best of both worlds: a QGraphicsObject that supports signals and slots, typically used in animation. QWidget, shown earlier in Figure 22.1 is another class that uses multiple inheritance from QObject.

Figure 22.3.  QGraphicsObject and Multiple Inheritance

QGraphicsObject and Multiple Inheritance

[Note]Note

In Figure 22.3, QObject is one of the base classes that is multiply inherited. One restriction Qt has is that QObject must be inherited only once by any class (virtual inheritance is not supported). Furthermore, the QObject-derived base must be listed first in the list of base classes. Breaking this rule can lead to strange errors from the code generated by the MetaObject compiler (moc).