views:

67

answers:

3

Greetings ,

I my QT application,I have a base class as follows.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 follows.I need to extend from QWidget because I need to implement event handling methods in all derived classes.(mouseMoveEvent(),keyPressEvent()..etc);

class IRzLayeringPlugin : public IRzPlugin , public QWidget{

}

Compiler this the error:

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
+1  A: 

The QObject base class is getting included more than once in the derived class. You need to use virtual base classes to solve the problem.

Naveen
That's not possible in this case since `QWidget` would need to derive virtually from `QObject`.
Job
+2  A: 

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.

Caleb Huitt - cjhuitt
thanks, thats what i did finally/
umanga
+2  A: 

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.

Venemo