Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct
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?
QList<QGraphicsItem *> items = scene->Items();
and then with
QGraphicsScene::removeItem(QGraphicsItem * item);
you might need to delete it yourself with
@mrjj sorry but i think that in that way is not possible because I am using QGraphicsTextItem, no QGraphicsItem
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
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.
just as a note
To avoid getting possible NULLs in the list, i would suggest doing
QGraphicsItem *item = qgraphicsitem_cast<QGraphicsItem*>(text); if (item) list.push_back(item);