Results 1 to 2 of 2

Thread: QGraphicsItem ambiguity in subclass

  1. #1
    Join Date
    Dec 2008
    Location
    Poland
    Posts
    383
    Thanks
    52
    Thanked 42 Times in 42 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Android

    Default QGraphicsItem ambiguity in subclass

    Hello,
    I have a question regarding design of multi subclasses item on the scene. I know that's not Qt question but C++ but because of the context I posted it in Qt section, if that's wrong section please move this thread.

    What I want to do is to have base class with all common code for the item like this:

    Qt Code:
    1. class Base : public virtual QGraphicsItem
    2. {
    3. // common code for all items on the scene
    4. };
    To copy to clipboard, switch view to plain text mode 
    Next I want some object that are i.e. QGraphicsPixmapItem, QGrapthicsTextItem etc, i.e..
    Qt Code:
    1. class ItemA : public Base, public QGraphicsPixmapItem
    2. {
    3. QRectF boundingRect() const {
    4. return QGraphicsPixmapItem::boundingRect();
    5. }
    6.  
    7. void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) {
    8.  
    9. painter->drawRoundedRect(-10, -10, 20, 20, 5, 5);
    10. QGraphicsPixmapItem::paint( painter, option, widget);
    11. }
    12.  
    13. // Only pixmap related code
    14. };
    To copy to clipboard, switch view to plain text mode 
    Now I want to control all these objects so I store pointers to objects like:
    Qt Code:
    1. QList <Base*> itemList;
    To copy to clipboard, switch view to plain text mode 
    The problem is ambiguity of QGraphicsItem (in Base) due to the fact that i.e. QGraphicsPixmapItem (and all Qt item classes) are declared as:
    Qt Code:
    1. class Q_WIDGETS_EXPORT QGraphicsPixmapItem : public QGraphicsItem
    To copy to clipboard, switch view to plain text mode 

    My question is how would I go about designing classes that do subclass from i.e. QGraphicsPixmapItem
    In the near future - corporate networks reach out to the stars. Electrons and light flow throughout the universe.
    The advance of computerization however, has not yet wiped out nations and ethnic groups.

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QGraphicsItem ambiguity in subclass

    There are a couple of options:

    1) share the common code via delegation instead of derivation, i.e. have a class that is used by all your items
    2) use a base class that is not a QGraphicsItem but which can return a pointer to the graphics item
    3) use a template as a base class that derives from the template type

    Cheers,
    _

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

    Talei (8th April 2014)

Similar Threads

  1. own subclass of QGraphicsItem problem
    By Azras in forum Newbie
    Replies: 1
    Last Post: 30th May 2013, 15:10
  2. Replies: 2
    Last Post: 15th April 2013, 06:33
  3. QGraphicsItem subclass compilation error
    By ^NyAw^ in forum Qt Programming
    Replies: 10
    Last Post: 30th January 2013, 14:55
  4. QGraphicsItem subclass and accessing custom properties
    By been_1990 in forum Qt Programming
    Replies: 4
    Last Post: 19th November 2010, 01:48
  5. QGraphicsItem subclass access to QGraphicsView size
    By rubenvb in forum Qt Programming
    Replies: 4
    Last Post: 23rd January 2010, 21:36

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.