Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

c++ - Multiple inheritance with qobject base

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...