With Qt 5.6 is it possible to avoid the diamond problem when signals and slots are defined in two branches?
-
Hi,
I would write a class A that implements some functions and declares some signals and slots. In my program, this class will be inherited by several other classes. Unfortunately, in a lot of cases these classes are already a QObject. So I'm facing about the classical diamond problem from QObject.Before, I worked with QT 4.8 that had no nice solution to this problem. But I do not know if a solution has been found in QT 5.6.
For the moment I have a (bad) solution consisting in:
class A { public: virtual void sigToto() = 0; virtual QObject * asQObject() = 0; protected: virtual void slotToto(); }; class B: public QObject, public A { public: Q_SIGNAL void sigToto() override final; QObject * asQObject() override final {return this;} protected: Q_SLOT voif slotToto() override final {A::slotToto();} };
And for the connections (performed in class A source code) I have to use the old-style syntax.
Some news to solve this (endless) problem?
-
You can inherit from QObject in class A, and then not inherit from QObject in class B, only declare Q_OBJECT macro there. Should work (class B will inherit from QObject through class A).
I guess, though, that you have only simplified a less straightforward situation, where indeed it is impossible to inherit from QObject twice. If so, then unfortunately (or fortunately... multiple inheritance is in general avoided) the situation remains (officially) unchanged - it's not possible and not supported. There are workarounds, however: https://forum.qt.io/topic/11864/multiple-inheritance-from-qobject-qobject-is-an-ambiguous-base-of/12
-
Hi @sierdzio,
Thanks. So it is still impossible to create a base class for a variety of QWidget-derived objects whether this class needs to define generic signals, slots, and connections. We still have to specialize each QWidget_derived object by duplicating common code. -
As long as you stick to pointer-to-member connects it should be possible. Depends on what you intend to do exactly. However I can count on my fingers the number of times that virtual multiple inheritance (i.e. the diamond) is really warranted, usually it is a sign of a bad design.