Multiple Inheritance & Qt
hello all,
I have met a problem while using the Multiple Inheritance with Qt with some codes like this:
Code:
class SomeClass: public ClassFromUI, public MyClassDrivedFromQWiget
{
Q_OBJECT
....
}
the class ClassFromUI is generated by the uic from a .ui file that i cannot change the content. It looks like:
the class MyClassDerivedFromQWidget writen by me, looks like:
Code:
class MyClassDerivedFromQWidget
: public virtual QWidget{
Q_OBJECT
....
}
when i compile them, i get the following errors:
Code:
...
...warning: virtual base 'QWidget' inaccessible in 'SomeClass' due to ambiguity
...error: 'QObject' is an ambiguous base of 'SomeClass'
Can anyone tell me, how to deal with this error???
thanks
kefeng
Re: Multiple Inheritance & Qt
From what I've always read/heard, you should never multiply inherit QObject, which is what you are doing here. If it's possible (which it should be), use a has-a or in-terms-of pattern, ie. make your derived widget a part of the Gui class, or something similar, whichever makes more sense, as I am not sure of the exact details of your situation.
Bojan
Re: Multiple Inheritance & Qt
I think you must inherit the QObject subclass 'before' other classes. That is, it should be the left-most class in your inheritance list.
But don't quote me on that. :)
Re: Multiple Inheritance & Qt
Quote:
Originally Posted by Michiel
I think you must inherit the QObject subclass 'before' other classes. That is, it should be the left-most class in your inheritance list.
But don't quote me on that. :)
You're right, but the OP's problem is that he is inheriting from QObject twice. Yoiu cannot do that.
Re: Multiple Inheritance & Qt
Ah, the dreaded diamond of death. I didn't notice. You should never inherit twice from the same class. I don't understand the inheritance tree enough to suggest a solution, though.
Re: Multiple Inheritance & Qt
first, thanks for all the answers.
sorry, i have not explained the problem accurately.
actually, the code is not writen by me. It's from another. In addition, it's a big project, i can not change the structure.
It is a project under windows, now i must export it to linux.
There was no problem with VC++.
By the way, I am now using gcc 4.0.2 under SUSE 10.
anyone an idea??:confused:
kefeng
Re: Multiple Inheritance & Qt
Similar situation as I'm in: http://www.qtcentre.org/forum/showthread.php?t=1027
If at all possible, redesign. Otherwise you're pretty much stuck.
A good possibility would be to make the object structure use a "has a" relationship, i.e. MyClassDerivedFromQWidget has a QWidget. This breaks the diamond structure.
Re: Multiple Inheritance & Qt
A way would be to make the low-level class inherits PRIVATEly from QObject thus there would be no instance of QObject in the inherited classes...
Re: Multiple Inheritance & Qt
Quote:
Originally Posted by kefeng.chen
There was no problem with VC++.
Highly unlikely. MOC will ot generate the correct code for multiple inheritance of QObject, regardless of the compiler/platform you are using.