Results 1 to 2 of 2

Thread: Multiple inheritance with QObject

  1. #1
    Join Date
    Feb 2008
    Posts
    98
    Thanks
    2
    Thanked 24 Times in 24 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Multiple inheritance with QObject

    Hi! I have some classes defining a tree structure.

    Qt Code:
    1. class BaseNode : public QObject;
    2. class SequentialNode : public BaseNode;
    3. class ParallelNode : public BaseNode;
    To copy to clipboard, switch view to plain text mode 

    BaseNode has a couple of signals and two slots, which are inherited by SequentialNode and ParallelNode. This is a general implementation of an algorithm.

    Now I would like to be able to subclass all this classes in order to make a custom implementation. So I'd do:

    Qt Code:
    1. class CustomBaseNode : public BaseNode;
    2. class CustomSequentialNode; // inherits... what?
    3. class CustomParallelNode; // inherits... what?
    To copy to clipboard, switch view to plain text mode 

    I would like to add one signal to CustomBaseNode, which is a reimplementation of BaseNode. Well, no problem here yet.

    CustomBaseNode should be inherited by CustomSequentialNode and CustomParallelNode following the same tree structure, so they should be subclasses of CustomBaseNode. Let's say:

    Qt Code:
    1. class CustomBaseNode : public BaseNode;
    2. class CustomSequentialNode : public CustomBaseNode;
    3. class CustomParallelNode : public CustomBaseNode;
    To copy to clipboard, switch view to plain text mode 

    But this way CustomSequentialNode is not a subclass of SequentialNode and doesn't have its funcionality. On the other hand, if I make it inherit SequentialNode it doesn't inherit the signal and functionality from CustomBaseNode.

    I could try to use multiple inheritance and make CustomSequentialNode inherit both CustomBaseNode and SequentialNode.

    Qt Code:
    1. class CustomBaseNode : public BaseNode;
    2. class CustomSequentialNode : public CustomBaseNode, public SequentialNode;
    3. class CustomParallelNode : public CustomBaseNode, public ParallelNode;
    To copy to clipboard, switch view to plain text mode 

    However, for CustomSequentialNode, both CustomBaseNode and SequentialNode are subclasses ofQObject. As far as I've understand from the Qt docs, QObject doesn't support multiple inheritance from more than one QObject.

    In the end, CustomSequentialNode would be inheriting BaseNode and QObject twice (once from SequentialNode - BaseNode - QObject and again from CustomBaseNode - BaseNode - QObject). I want to use signals, so I need QObject.

    Is there some design pattern or standard way to solve this issue?

    Thanks.

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Multiple inheritance with QObject

    This won't work this way. Use the "bridge" design pattern.

  3. The following user says thank you to wysota for this useful post:

    victor.fernandez (22nd April 2010)

Similar Threads

  1. Possibly an inheritance problem with QObject?
    By chadkeck in forum Qt Programming
    Replies: 8
    Last Post: 5th November 2008, 01:26
  2. Multiple Inheritance & Qt
    By kefeng.chen in forum Qt Programming
    Replies: 8
    Last Post: 21st March 2006, 18:37
  3. Multiple inheritance & Qt
    By dublet in forum Qt Programming
    Replies: 11
    Last Post: 8th March 2006, 08:12
  4. Multiple Inheritance
    By sunil.thaha in forum General Programming
    Replies: 4
    Last Post: 21st February 2006, 04:00
  5. subclassing/inheritance QObject problem
    By cbeall1 in forum Qt Programming
    Replies: 12
    Last Post: 13th February 2006, 17:49

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.