Results 1 to 9 of 9

Thread: Multiple Inheritance & Qt

  1. #1

    Default Multiple Inheritance & Qt

    hello all,

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

    Qt Code:
    1. class SomeClass: public ClassFromUI, public MyClassDrivedFromQWiget
    2. {
    3. Q_OBJECT
    4. ....
    5. }
    To copy to clipboard, switch view to plain text mode 

    the class ClassFromUI is generated by the uic from a .ui file that i cannot change the content. It looks like:
    Qt Code:
    1. class ClassFromUI : public QWidget
    2. {
    3. Q_OBJECT
    4. ....
    5. }
    To copy to clipboard, switch view to plain text mode 

    the class MyClassDerivedFromQWidget writen by me, looks like:

    Qt Code:
    1. class MyClassDerivedFromQWidget : public virtual QWidget
    2. {
    3. Q_OBJECT
    4. ....
    5. }
    To copy to clipboard, switch view to plain text mode 

    when i compile them, i get the following errors:
    Qt Code:
    1. ...
    2. ...warning: virtual base 'QWidget' inaccessible in 'SomeClass' due to ambiguity
    3. ...error: 'QObject' is an ambiguous base of 'SomeClass'
    To copy to clipboard, switch view to plain text mode 

    Can anyone tell me, how to deal with this error???

    thanks

    kefeng

  2. #2
    Join Date
    Jan 2006
    Location
    N.B. Canada
    Posts
    47
    Thanked 8 Times in 7 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default 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
    The march of progress:
    C:
    printf("%10.2f", x);
    C++:
    cout << setw(10) << setprecision(2) << showpoint << x;
    Java:
    java.text.NumberFormat formatter = java.text.NumberFormat.getNumberInstance();
    formatter.setMinimumFractionDigits(2);
    formatter.setMaximumFractionDigits(2);
    String s = formatter.format(x);
    for (int i = s.length(); i < 10; i++) System.out.print(' ');
    System.out.print(s);

  3. #3
    Join Date
    Mar 2006
    Location
    The Netherlands
    Posts
    300
    Thanks
    9
    Thanked 29 Times in 29 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default 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.
    "The strength of a civilization is not measured by its ability to wage wars, but rather by its ability to prevent them." - Gene Roddenberry

  4. #4
    Join Date
    Jan 2006
    Location
    Mountain View, CA
    Posts
    279
    Thanked 42 Times in 37 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default 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.
    Save yourself some pain. Learn C++ before learning Qt.

  5. #5
    Join Date
    Mar 2006
    Location
    The Netherlands
    Posts
    300
    Thanks
    9
    Thanked 29 Times in 29 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default 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.
    "The strength of a civilization is not measured by its ability to wage wars, but rather by its ability to prevent them." - Gene Roddenberry

  6. #6

    Default 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??

    kefeng

  7. #7
    Join Date
    Mar 2006
    Posts
    22
    Qt products
    Qt3
    Platforms
    Windows

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

  8. #8
    Join Date
    Jan 2006
    Location
    travelling
    Posts
    1,116
    Thanks
    8
    Thanked 127 Times in 121 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default 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...
    Current Qt projects : QCodeEdit, RotiDeCode

  9. #9
    Join Date
    Jan 2006
    Location
    Mountain View, CA
    Posts
    279
    Thanked 42 Times in 37 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default 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.
    Save yourself some pain. Learn C++ before learning Qt.

Similar Threads

  1. Object and multiple inheritance for interfaces
    By brcain in forum Qt Programming
    Replies: 8
    Last Post: 29th June 2021, 15:29
  2. Multiple Inheritance for QGraphicsItem
    By pankaj.patil in forum Qt Programming
    Replies: 2
    Last Post: 1st July 2008, 14:49
  3. QThread and Multiple inheritance
    By ^NyAw^ in forum General Programming
    Replies: 6
    Last Post: 10th January 2008, 10:50
  4. Multiple inheritance & Qt
    By dublet in forum Qt Programming
    Replies: 11
    Last Post: 8th March 2006, 08:12
  5. Multiple Inheritance
    By sunil.thaha in forum General Programming
    Replies: 4
    Last Post: 21st February 2006, 04:00

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.