Using Qt signals and slots with multiple inheritance
I need to use multiple inheritance because I need to (1) implement an “is-a” relationship from QGraphicsItem, and (2) implement an application-specific interface (MyInterface):
I would like to connect to signals from pointers to the interface, i.e.:
- MyInterface *my_interface_instance = GetInstance();
- connect(my_interface_instance, SIGNAL(MyInterfaceSignal()), this, SLOT(SomeSlot()));
Problem: To expose signals in MyInterface, it needs to derive from QObject. But then I have multiple-inheritance from QObject, which is not allowed. The solutions on this article [doc.trolltech.com] are IMO fairly useless in the common case (accessing an object through its interface), because you cannot connect the signals and slots from MyInterface* but must cast it to the derived-type. Since MyClass is one of many MyInterface-derived classes, this would necessitate “code-smelly” if-this-cast-to-this-else-if-that-cast-to-that statements and defeats the purpose of the interface.
The solution below seems to work, but I was hoping a Troll could let me know if this is supported or is going to cause undefined behavior. If I dynamic_cast a MyInterface* to QObject* (because I know all MyInterface-derived classes also inherit eventually from QObject), it seems to work:
- class MyInterface {
- protected:
- virtual void MyInterfaceSignal() = 0;
- };
- Q_OBJECT
- signals:
- void MyInterfaceSignal();
- };
Usage:
- MyInterface *my_interface_instance = GetInstance();
- connect(dynamic_cast<QObject*>(my_interface_instance), SIGNAL(MyInterfaceSignal()), this, SLOT(SomeSlot()));
Is this a bad idea?
3 replies
We had a similar discussion recently, and one of the options would be that in your interface’s constructor, you’d take a QObject* of the actual instance.
Your class’s constructor would look like:
- MyClass::MyClass()
- : MyInterface(this) {}
That way, you could use the QObject pointer in your interface to connect signals and slots.
Also, you could take a look at QDBusAbstractAdaptor [doc.qt.nokia.com] that basically “attaches” an interface to any given QObject.
You must log in to post a reply. Not a member yet? Register here!



