Results 1 to 2 of 2

Thread: Unable to get QList of custom QGraphicsItems (newbie) from scene

  1. #1
    Join Date
    Feb 2008
    Posts
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Unable to get QList of custom QGraphicsItems (newbie) from scene

    Hi,
    I have created a subclass from QGraphicsItem

    Qt Code:
    1. class Node : public QGraphicsItem{
    2. };
    To copy to clipboard, switch view to plain text mode 
    and added few nodes using following code to a QGraphicsscene

    Qt Code:
    1. for(int j=0;j<no_of_nodes;j++){
    2. Node *node = new Node();
    3. node->apply_color(QColor(Qt::green));
    4. node->setPos(0,0);
    5. node->apply_text(node_name[j]);
    6. scene->addItem(node);
    7. }
    To copy to clipboard, switch view to plain text mode 
    Now i want to access these nodes later for changing color (apply_color() function defined in Node). I looked at a function called
    Qt Code:
    1. QList<QGraphicsItem *> scene->items();
    To copy to clipboard, switch view to plain text mode 
    so i tried to get the list of graphicalitems using
    Qt Code:
    1. QList<Node *> listofitems = scene->items();
    To copy to clipboard, switch view to plain text mode 
    but im getting the following error
    conversion from ‘QList<QGraphicsItem*>’ to non-scalar type ‘QList<Node*>’ requested
    And how does the QList<QGraphicsItem *> scene->items(); return items ??? in the same order as they are added???
    I have also uploaded the code

    Thanx
    Last edited by jpn; 10th September 2008 at 16:44. Reason: missing [code] tags

  2. #2
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Unable to get QList of custom QGraphicsItems (newbie) from scene

    You cannot directly convert between two template containers which contain different types.
    Qt Code:
    1. QList<QGraphicsItem *> items = scene->items();
    2. foreach (QGraphicsItem* item, items)
    3. {
    4. Node* node = dynamic_cast<QGraphicsItem*>(item);
    5. if (node)
    6. {
    7. ...
    8. }
    9. }
    To copy to clipboard, switch view to plain text mode 
    See also qgraphicsitem_cast for more efficient casting. It just requires support from Node class.
    J-P Nurmi

Similar Threads

  1. How to Compile VTKDesigner2 with Qt?
    By alfredoaal in forum Newbie
    Replies: 0
    Last Post: 5th September 2008, 05:34

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.