Remove QGraphicsTextItem from QGraphicsScene
-
Hello! I am showing numbers with QGraphicsTextItem in a QGraphicsSene. Now i want to remove the last element of them.
I was thinking in storage every items in a QList<QGraphicsItem*>, get the last element and remove it with some function, but i am no be able to find that kind of function. Any suggerency?
-
Hi
Like ?QList<QGraphicsItem *> items = scene->Items();
and then with
http://doc.qt.io/qt-5/qlist.html#last
and
QGraphicsScene::removeItem(QGraphicsItem * item);
you might need to delete it yourself with
delete item;
check docs. -
@AdrianCruz
Hi
The scene store items as base type (QGraphicsItem) .
Even they are also subclasses types. Like QGraphicsTextItem.
QGraphicsTextItem is also a QGraphicsItem.
So you can just cast them using
http://doc.qt.io/qt-5/qgraphicsitem.html#qgraphicsitem_cast
and if cast is ok (return is not null) then , its that type. and you can delete it. -
Finally, i did a cast how you said and now is working. Thanks so much!
-
If someone has the same problem, i did:
QGraphicsItem *item = qgraphicsitem_cast<QGraphicsItem*>(text); list.push_back(item);
list is a QList<QGraphicsItem*> list.
-
@AdrianCruz
hi
Super :)
just as a note
To avoid getting possible NULLs in the list, i would suggest doingQGraphicsItem *item = qgraphicsitem_cast<QGraphicsItem*>(text); if (item) list.push_back(item);