Results 1 to 2 of 2

Thread: QGraphicsItemPrivate and qgraphicsitem_cast

  1. #1
    Join Date
    Jan 2009
    Posts
    47
    Thanks
    6
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default QGraphicsItemPrivate and qgraphicsitem_cast

    Hi,

    I have a QGraphicsTextItem custom object that contains QGraphicsRectItem(s). In my scene's mouseReleaseEvent, I have code to look through the QList<QGraphicsItem *> and only act when the item is of a specific type, Port::Type which is enum { Type = UserType + 16 };

    I notice that my QList<QGraphicsItem *> also contains the QGraphicsItemPrivate d_ptr when I select on the QGraphicsTextItem custom object itself, resulting in a crash on the "int aType = anItem->type();" line

    My questions are:
    1) why the d_ptr is returned in my items() call
    2) how am I going to modify my following code to just look for a specific type, Port *, but ignore everything else in the items() call. Thanks

    Qt Code:
    1. Port *oport = 0;
    2. while (!startItems.isEmpty()) {
    3. QGraphicsItem* anItem = startItems.takeFirst();
    4. if (anItem) {
    5. int aType = anItem->type();
    6. if (aType == Port::Type) {
    7. oport = qgraphicsitem_cast<Port *>(anItem);
    8. break;
    9. }
    10. }
    11. }
    12.  
    13. where Port is a QGraphicsRectItem
    To copy to clipboard, switch view to plain text mode 

  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: QGraphicsItemPrivate and qgraphicsitem_cast

    Qt Code:
    1. QList<QGraphicsItem*> allItems = scene->items();
    2. QList<Port*> portItems;
    3. foreach(QGraphicsItem *item, allItems){
    4. Port *portItem = qgraphicsitem_cast<Port*>(item);
    5. if(portItem) portItems << portItem;
    6. }
    To copy to clipboard, switch view to plain text mode 

    Where:
    Qt Code:
    1. class Port : public QGraphics...Item {
    2. public:
    3. enum { Type = UserType+16 };
    4. int type() const { return Type; }
    5. };
    To copy to clipboard, switch view to plain text mode 
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


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.