Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the correct way of Multiple inheritance in Qt/C++?

In my Qt application, I have a base class as follow. I am using QObject because I want to use Signal-Slot mechanism in all derived classes.

class IRzPlugin : public QObject {

public:
  virtual void registerMenu(QWidget*);
  virtual void execute();
}

Then I have another class as follow. I need to extend from QWidget because I need to implement event handling methods in all derived classes (such as mouseMoveEvent(), keyPressEvent() and others).

class IRzLayeringPlugin : public IRzPlugin , public QWidget{

}

But compiler gives these the errors:

C:\svn\osaka3d\tags\iter08\prototype\osaka3d\rinzo\plugin\moc_IRzLayeringPlugin.cxx: In member function `virtual const QMetaObject* IRzLayeringPlugin::metaObject() const':
C:\svn\osaka3d\tags\iter08\prototype\osaka3d\rinzo\plugin\moc_IRzLayeringPlugin.cxx:51: error: `QObject' is an ambiguous base of `IRzLayeringPlugin'
C:\svn\osaka3d\tags\iter08\prototype\osaka3d\rinzo\plugin\moc_IRzLayeringPlugin.cxx:51: error: `QObject' is an ambiguous base of `IRzLayeringPlugin'
make[2]: *** [CMakeFiles/Rinzo.dir/plugin/moc_IRzLayeringPlugin.cxx.obj] Error 1
like image 673
Ashika Umanga Umagiliya Avatar asked Jul 08 '10 06:07

Ashika Umanga Umagiliya


People also ask

Which is correct for multiple inheritance?

Which among the following is correct for multiple inheritance? Explanation: Class topper is getting derived from 2 other classes and hence it is multiple inheritance. Topper inherits class stream and class student publicly and hence can use its features.

What is the inheritance in C Write the example for multiple inheritance?

Multiple Inheritance in C++ Multiple Inheritance is a feature of C++ where a class can inherit from more than one classes. The constructors of inherited classes are called in the same order in which they are inherited. For example, in the following program, B's constructor is called before A's constructor.

Can we achieve multiple inheritance in C?

Master C and Embedded C Programming- Learn as you goMultiple inheritance occurs when a class inherits from more than one base class. So the class can inherit features from multiple base classes using multiple inheritance. This is an important feature of object oriented programming languages such as C++.

Does QWidget inherit from QObject?

QWidget inherits QObject and QPaintDevice The QObject class is the base class of all Qt objects. QObject is the heart of the Qt object model providing support for signals, slots, and the meta object system. The QPaintDevice class is the base class of objects that can be painted on with QPainter.


2 Answers

In the current incarnation, it isn't possible to use QObject in multiple inheritance paths for a derived class (like your IRzLayeringPlugin class). The only solution I've ever seen was to create an interface class without any QObject inheritence, but with functions that directly correspond to the QObject functions you want to use, then implement the bridge between the interface and your other QObject class inheritance in your specific class. It gets ugly fairly quickly.

like image 57
Caleb Huitt - cjhuitt Avatar answered Oct 03 '22 10:10

Caleb Huitt - cjhuitt


There was a similar question today here.

Basically, two things are needed:

  • Adding Q_DECLARE_INTERFACE after the interface class declaration
  • Adding the interface to the Q_INTERFACES macro of the class

After this, qobject_cast will work with your interfaces.

If you'd like to use signals and slots from the interfaces, you are out of luck, because you can only do that with types that derive from QObject. But in this case, you would always get the 'QObject' is an ambiguous base of 'IRzLayeringPlugin' error.

In this case, @Caleb's idea is still the best.

like image 32
Venemo Avatar answered Oct 03 '22 10:10

Venemo