
Originally Posted by
hb
This code however will have problems if some of the QGraphicsItems are children of other QGraphicsItems. As the children will automatically be destroyed with their parents, you'll get double frees and program crashes.
Actually, thinking about it it's probably cleaner to first get a list of all top-level items, and then use the method jacek described to clear that list instead of the whole items() list.
Code to get a list of all top-level items could look like this:
QList<QGraphicsItem*> MyGraphicsScene::getTopLevelItems()
{
int numItems, iItem;
QList<QGraphicsItem*> topLevel;
QList<QGraphicsItem*> itemList = items();
numItems = itemList.size();
for (iItem = 0; iItem < numItems; iItem++) {
if (item->parentItem() == NULL)
topLevel.append(item);
}
return topLevel;
}
QList<QGraphicsItem*> MyGraphicsScene::getTopLevelItems()
{
int numItems, iItem;
QList<QGraphicsItem*> topLevel;
QList<QGraphicsItem*> itemList = items();
numItems = itemList.size();
for (iItem = 0; iItem < numItems; iItem++) {
QGraphicsItem *item = itemList.at(iItem);
if (item->parentItem() == NULL)
topLevel.append(item);
}
return topLevel;
}
To copy to clipboard, switch view to plain text mode
Bookmarks