lundi 14 janvier 2008

Multiple inheritance and Q_OBJECT

I found out today, while working on porting a Motif application to Qt, that when you are inheriting from more than one class and not all of them are derived from QObject, you should make sure the first one does inherit QObject if you want to use the Q_OBJECT macro. For instance:

class A : public QObject {};
class B {};

class C : public A, public B
{
Q_OBJECT
};

If you do it the other way, (class C : public B, public A) you will get errors inside the moc_C.cxx file saying that it couldn't find the QMetaObject members of class B (which it doesn't have) because moc only looks at the first base class listed.
There all sorts of other little things like this which you can read about in the Qt moc documentation (for instance, nested classes cannot have signals or slots).

Aucun commentaire: