Results 1 to 2 of 2

Thread: Multiple inheritance of QObjects

  1. #1
    Join Date
    Jun 2012
    Posts
    98
    Thanks
    11
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Multiple inheritance of QObjects

    I've been getting crafty, and it worked until I needed to multipally inherit (I realize that's not a word )

    The craftyness is that I've abstracted a bunch of boilerplate code to base classes that mean any newb can create a class derived from them and they'll automatically hook up to the desired COM interface and all COM events will route their important information to a virtual function (that obv.s needs to be implemented by them to use said events.)

    The problem with said craftiness is if I need to speak to multiple interfaces in COM I would either need to create new combinations of these interfaces for each possible combination (at least, that occurs) rather than simply inherit from both. It looks like this:

    Qt Code:
    1. class A : public QWidget
    2. {
    3. Q_OBJECT
    4. //..
    5. }
    6.  
    7. class B: public QWidget
    8. {
    9. Q_OBJECT
    10. //..
    11. }
    12.  
    13. class C: public A, public B //doesn't work obviously
    14. {
    15. Q_OBJECT
    16. //..
    17. }
    To copy to clipboard, switch view to plain text mode 

    I'm having trouble finding online how you work around this; which IIRC is declaring something like:
    Qt Code:
    1. class A : private QWidget
    2. {
    3. Q_OBJECT
    4. //..
    5. }
    6.  
    7. class B: private QWidget
    8. {
    9. Q_OBJECT
    10. //..
    11. }
    12.  
    13. class C: public A, public B //,public QWidget?
    14. {
    15. Q_OBJECT //<- I noticed some confusion in another thread about whether this was necessary
    16. //..
    17. }
    To copy to clipboard, switch view to plain text mode 

    but... this doesn't work (with or without the public QWidget; or at least, it has warnings either way.)



    As to the inevitable "why are you doing this again..?" question: These base classes are Q_OBJECTs that take in another class and automatically hook up the COM event handler's released SIGNAL with their relevant SLOT, meaning that no code has to be written for a new class (when correctly inheriting) in order to listen to COM events relevant to your object. Were I to take out the Q_OBJECT declaration I would have to write the connection code each time in a manager class that handles these object's lifetimes, which is fine.. but it's less elegant and does require extra time needlessly.



    So my fellow C++/Qt wizards, what should I do..? (I did look at the other multiple inheritance threads that popped up, but they didn't seem to address this specific problem; nor has google been kind this time around.)


    Added after 34 minutes:


    Ah, it looks like virtual inheritance is what I was looking for; though this still doesn't work with QWidget derivation as it complains when it hits .moc files that you're converting from a QObject* to a MyWidget* and that doing so virtually isn't possible.


    Added after 23 minutes:


    With some more research it would seem that the problem is untenable due to Qt not supporting virtual inheritance in moc and this being the Diamond Problem.

    The solution I believe I'll be taking (for those who run across this in the future) looks like this:

    -I have a Manager class that already hands these objects out (and manages their lifetimes, etc..)
    -because the Manager has all of the objects that need these signals and I wish to not have to change code when adding more of these objects; but these are the ONLY Qt objects it holds, I can utilize it's child list to walk through and cast each of these object's pointers to the type of object I need and simply deliver the "signals" manually. Another option would be to change the derived class's version of the virtual function to a slot (though I am only *assuming* that that would work.)
    Last edited by tescrin; 7th January 2013 at 23:09.

  2. #2
    Join Date
    Sep 2011
    Posts
    1,241
    Thanks
    3
    Thanked 127 Times in 126 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Multiple inheritance of QObjects

    First question - do you really need QWidgets? Or just QObjects?

    Comment: Just making the inheritance private will not work + is not correct. You need to use virtual inheritance to resolve 'the dreaded diamond'.

    Since Qt doesn't like this (apparently) then you could perhaps get around it by doing something like this:
    Qt Code:
    1. // A interface
    2. class IA
    3. {
    4. // methods in A
    5. };
    6.  
    7. class A : public IA, public QWidget
    8. {
    9. // your implementations
    10. };
    11.  
    12. // B interface
    13. class IB
    14. {
    15. // methods in A
    16. };
    17.  
    18. class B : public IB, public QWidget
    19. {
    20. // your implementations
    21. };
    22.  
    23. class IC : public IA, public IB
    24. {
    25. };
    26.  
    27.  
    28. class C : public IC, public QWidget
    29. {
    30. public:
    31. // IB is implemented by b
    32. // IA is implemented by a
    33.  
    34. // e.g.
    35. /*
    36.   void foo_a() {a.foo_a();}
    37.   void foo_b() {b.foo_b();}
    38. */
    39.  
    40. private:
    41. A a;
    42. B b;
    43. };
    To copy to clipboard, switch view to plain text mode 
    Last edited by amleto; 8th January 2013 at 00:08.
    If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

Similar Threads

  1. Multiple inheritance question
    By marcvanriet in forum Newbie
    Replies: 4
    Last Post: 4th October 2010, 11:30
  2. Multiple Inheritance for QGraphicsItem
    By pankaj.patil in forum Qt Programming
    Replies: 2
    Last Post: 1st July 2008, 15:49
  3. Multiple Inheritance & Qt
    By kefeng.chen in forum Qt Programming
    Replies: 8
    Last Post: 21st March 2006, 19:37
  4. Multiple inheritance & Qt
    By dublet in forum Qt Programming
    Replies: 11
    Last Post: 8th March 2006, 09:12
  5. Multiple Inheritance
    By sunil.thaha in forum General Programming
    Replies: 4
    Last Post: 21st February 2006, 05: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.