Unable to get QList of custom QGraphicsItems (newbie) from scene
Hi,
I have created a subclass from QGraphicsItem
and added few nodes using following code to a QGraphicsscene
Code:
for(int j=0;j<no_of_nodes;j++){
Node *node = new Node();
node
->apply_color
(QColor(Qt
::green));
node->setPos(0,0);
node->apply_text(node_name[j]);
scene->addItem(node);
}
Now i want to access these nodes later for changing color (apply_color() function defined in Node). I looked at a function called
Code:
QList<QGraphicsItem *> scene->items();
so i tried to get the list of graphicalitems using
Code:
QList<Node *> listofitems = scene->items();
but im getting the following error
Quote:
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
Re: Unable to get QList of custom QGraphicsItems (newbie) from scene
You cannot directly convert between two template containers which contain different types.
Code:
QList<QGraphicsItem *> items = scene->items();
{
Node* node = dynamic_cast<QGraphicsItem*>(item);
if (node)
{
...
}
}
See also qgraphicsitem_cast for more efficient casting. It just requires support from Node class.