PDA

View Full Version : Multiple Inheritance & Qt



kefeng.chen
20th March 2006, 23:06
hello all,

I have met a problem while using the Multiple Inheritance with Qt with some codes like this:



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:


class ClassFromUI : public QWidget
{
Q_OBJECT
....
}


the class MyClassDerivedFromQWidget writen by me, looks like:



class MyClassDerivedFromQWidget : public virtual QWidget
{
Q_OBJECT
....
}


when i compile them, i get the following errors:


...
...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

Bojan
20th March 2006, 23:38
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

Michiel
20th March 2006, 23:44
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. :)

Chicken Blood Machine
21st March 2006, 04:50
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.

Michiel
21st March 2006, 07:33
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.

kefeng.chen
21st March 2006, 09:02
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

dublet
21st March 2006, 09:04
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.

fullmetalcoder
21st March 2006, 14:53
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...

Chicken Blood Machine
21st March 2006, 18:37
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.