Results 1 to 3 of 3

Thread: Multiple Inheritance for QGraphicsItem

  1. #1
    Join Date
    Jun 2008
    Posts
    8
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default Multiple Inheritance for QGraphicsItem

    Hello,
    I am using QT 4.4.0 for designing a circuit editor. I am using Qt's Graphics View framework for drawing different elements of circuit, text, pixmaps and even plots (embedding QwtPlotWidget in QGraphicsProxyWidget class).
    I want to have these different elements derived from a common class, lets call it base, and move the functionality common to them in base. Base needs to reimplement some of the methods defined in QGraphicsItem class (itemChange() to mention one), and hence has to derive from QGraphicsItem class. However, this design leads to diamond inheritance issue (as the classes deriving from base also have to derive from classes like QGraphicsPixmapItem and QGraphicsProxyWidget which derive from QGraphicsPixmapItem). Unfortunately, none of the classes given by Qt which derive from QGraphicsPixmapItem (like QGraphicsPixmapItem) inherit it virtually, and put a lot of restriction on my design. Can anyone please suggest me a solution or workaround?

    Thanks in advance,
    Pankaj

    Given below is the pseudo code depicting my problem:

    Qt Code:
    1. class Element : public QGraphicsPixmapItem
    2. {
    3. //Lots of common code
    4. QVariant itemChange ( GraphicsItemChange change, const QVariant & value )
    5. { //Update certain common data upon state change, common to all}
    6. }
    7.  
    8. class TextElement : public Element, public QGraphicsTextItem
    9. {
    10. }
    11.  
    12. class Plot : public QwtPlotWidget, public Element
    13. {
    14. }
    15.  
    16. class scene : public QGraphicsScene
    17. {
    18. TextElement *t = new TextElement;
    19. Plot *p = new Plot;
    20. addItem( t); // Problem: Ambiguous conversion from TextElement* to QGraphicsItem*
    21. addItem(p);//Problem :Ambiguous conversion from Plot* to QGraphicsItem*
    22. //Also, problem while directly or indirectly accessing itemChange() ( ambiguous access)
    23. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by jpn; 1st July 2008 at 08:10. Reason: missing [code] tags

  2. #2
    Join Date
    Jun 2008
    Location
    Glenwood, NJ USA
    Posts
    32
    Thanks
    1
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11

    Default Re: Multiple Inheritance for QGraphicsItem

    With multiple inheritance, don't you need to fully qualify:

    QGraphicsItem::addItem() <- example.

    If both the base classes have the same member function, this is necessary.

  3. #3
    Join Date
    Jun 2008
    Posts
    8
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Multiple Inheritance for QGraphicsItem

    Hi awhite,
    Thanks for your reply. Your are right that if both the base classes have same member function, I would have to fully qualify who's function I want to use.
    My problem is more of diamond inheritance issue. TextElement, for example, derives from Element and QGraphicsTextItem, both ultimately derive from QGraphicsItem ( I misspelt class Element : public QGraphicsPixmapItem, rather it should be class Element : public QGraphicsItem, however, even if it was to derive from QGraphicsItem, the same issue would come as it is ultimately deriving from QGraphicsItem through QGraphicsPixmapItem). Hence the conflict comes.
    Fully qualified syntax would solve the problem, as would virtual inheritance. The problem with fully qualified syntax is too much of code rewriting, while for virtual inheritance is that QGraphicsTextItem (and QGraphicsPixmapItem in case Element derives from it) does not virtually inherit QGraphicsItem, limiting the use of virtual inheritance.
    Is there a workaround for this problem?
    -Thanks,
    Pankaj

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. QThread and Multiple inheritance
    By ^NyAw^ in forum General Programming
    Replies: 6
    Last Post: 10th January 2008, 10:50
  3. Multiple Inheritance & Qt
    By kefeng.chen in forum Qt Programming
    Replies: 8
    Last Post: 21st March 2006, 18:37
  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.