I want to create a inheritance system in one of my Qt applications where there is a QObject derived class "Abstract1" that implements two signals and its derived classes, most of which will be QWidget derived.
The problem is that, the way I'm doing, is leading to "error: 'QObject' is an ambiguous base of " errors. A research on SO revealed the reason for that: I'm including two instance of QObject, one coming from my "Abstract1" class and the other from "QWidget":
class MyNewClass : public Abstract1, public QWidget
My question: how can I avoid the problem but still maintain the structure I want? That is, pure QObject class with the signals and subclasses which include this one and are also QWidget derived?
Related
I am writing a series of custom classes for Qt. What I need is to have a base class that has few custom signals and slots, and children classes will have it. However, I do know that inheriting from the same classes will be thrown an error. I have read the documentation but only dictates that I need to include QObject if I wish to use Q_OBJECT macro. This is the following sample code that I intend to do:
class Base : public QObject
{
Q_OBJECT
base signals here
public base slots here
}
class Child : public QLabel, public Base
{
// Other codes here
}
Will it be possible this way? Since I only wish to use Qt to connect with all children inherits from the parent class.
Multiple inheritance from QObject will not work for several reasons.
(Moc-Compiler does not work correctly and QObject has member variables which makes mutliple inheritance unsafe).
You better work with composition instead of inheritance in case you want to provide the same behavious to several objects.
in my Qt multithread programm I want to implement a QObject-based baseclass, so that every class derived from it cann use its signals and slots (for example to throw an error).
I implemented MyQObject : public QObject{...}. But for classes that derive from QWidget I cannot multi inher from QWidget and MyQObject.
I can solve the problem by calling the slot directly by QMetaObject::invokeMethod(...), but i am interested if there would be another way to solve this problem.
Multiple inheritance with QObject can be done following these rules:
Only one of the parents class can be a QObject/QWidget
The QObject parent have to be the first parent in the initialization.
There's a trick that allows you to declare a signal like function in a non-qobject interface and then declaring it inheriting that interface using composition. Take a look to this post, it may be useful.
I have multiple QObject subclasses which should act as interface classes and be implemented by (inherited by) some other classes. For example let class A : virtual public QObject and class B : virtual public QObject be interface classes. I need a QDialog object to implement their behavior like: class X: public QDialog, A, B.
Unfortunately I did not read documentation carefully at design time and now I realized two facts:
implementing slots as pure virtual member functions is not possible because moc-generated code will need to call them.
Multiple inheritance is not supported for QObject-derived classes. That's not a diamond thing. It's because moc-generated code can't static_cast a virtual QObject object to a A* via virtual base. (That's what compiler says!)
What is best possible alternative design to which affects code as less as possible? I can think of macro hacks. Maybe a macro in base class (like Q_OBJECT) to copy all members, signals, slots of base to derived class?
Note That's really bad that QObjects can't be inherited multiple times. Isn't?
If you really need to expose QObject member functions through your A and B classes create an abstract base class (i.e. a class with only pure virtual member functions), say AbstractQObject, and re-declare there the QObject member functions you need to expose.
Have A and B derive virtually from AbstractQObject and X from QDialog, A and B.
This should solve the problem you described, but I suspect you would be better off redesigning your code.
I understand that in general, multiple inheritance from QObject-derived classes (even virtual multiple inheritance) is not supported in Qt.
I understand the reason to be (I think) that even in the virtual inheritance case, the Qt classes do not themselves virtually inherit from QObject. For example, if you attempt to derive a class virtually from both QWidget and QThread, this is placing the virtual inheritance in an irrelevant place in the inheritance chain and you still wind up with two QObject instances.
I therefore think it is safe, and supported in Qt, to use virtual inheritance where the ONLY Qt class being derived from is QObject itself.
I have:
class Top : public QObject {};
class Left : public virtual Top {};
class Right : public virtual Top {};
class Bottom : public Left, public Right {}; // Is this safe, and supported by Qt?
Note that instances of Bottom truly have only one instance of Top (and hence only one instance of QObject), so it seems that the rationale for avoiding multiple inheritance in Qt (even virtual multiple inheritance) does not apply here.
The above construct nonetheless results in the Qt compiler warning Class Bottom inherits from two QObject subclasses Left and Right. This is not supported!.
Am I correct? Is it safe to ignore the Qt compiler warning in this specific scenario? Is the above construct, involving virtual multiple inheritance directly from QObject, safe and supported in Qt?
No, multiple inheritance from QObject is not supported by Qt in any way.
The problem is not with virtual inheritance, it's Qt's meta-object system. Each QObject base class has an associated QMetaObject which manages signals, slots, properties, etc, and each meta-object knows its parent QObject so e.g. signals which exist in parent classes can be handled. The Qt moc is not able to deal with multiple inheritance from QObject or any of its sub-classes.
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
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.
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.