8

Example of code:

class TestOne : public QWidget // To fix this i need to modify  class QWidget : public virtual QObject{}; which belongs to qt
{
  // ...
};

class TestTwo : public virtual QObject
{
  // ...
};

class Test : public TestOne, public TestTwo
{
 // ...
};

What are other ways to get around this problem?

DEgITx
  • 960
  • 1
  • 13
  • 24
  • 4
    The best way is not to use multiple inheritance with QObject in the mix. Try redesigning so you don't need it. – Mat Aug 26 '12 at 15:37
  • MI from QObject isn't supported with Qt (as far as I remember); use plain aggregation + member call propagation to get the second class' behavior into the resulting class. – mlvljr Aug 26 '12 at 15:50

1 Answers1

12

QObject is not designed for multiple inheritance. QObject doesn't support multiple inheritance from another QObjects. If you inherit from two Classes only the first one can be QObject and second not as per http://qt-project.org/doc/qt-4.8/moc.html

Virtual inheritance with QObject is not supported.

You can make association between two QObjects and forward signals between them.

You can abstract your common functionalities in a way that doesn't require a signal/slot and don't inherit that from QObject. and then inherit from it. and then mix that QObject free class in MI with your your class. You can forward calls to those inherited methods through signals/slots from Derived QObject

Neel Basu
  • 12,638
  • 12
  • 82
  • 146
  • Why not call methods directly? You probably meant forwarding slot invokations end signals emission. Or not? – mlvljr Aug 26 '12 at 21:18
  • Yes You grab the signal in your QObject inherited class's slot and then call the method of non-QObject Class. – Neel Basu Aug 27 '12 at 09:01