... I got the point, obviously I can't delete an object and use it later... anyway if it can be of some interest toanyone, this is the code of the working function that deletes all the children of a QStandardItem and recusrively all the "younger" relatives, avoiding a memory leak.
//delete all children of parent;
QList<QStandardItem *> carryItems; //Last In First Out stack of items
QList<QStandardItem *> itemsToBeDeleted; //List of items to be deleted
while (loopItem->rowCount())
{
itemsToBeDeleted << loopItem->takeRow(0);
//if the row removed has children:
if (itemsToBeDeleted.at(0)->hasChildren())
{
carryItems << loopItem; //put on the stack the current loopItem
loopItem = itemsToBeDeleted.at(0); //set the row with children as the loopItem
}
//if current loopItem has no more rows but carryItems list is not empty:
if (!loopItem->rowCount() && !carryItems.isEmpty()) loopItem = carryItems.takeFirst();
}
qDeleteAll(itemsToBeDeleted);
//delete all children of parent;
QStandardItem * loopItem = parent; //main loop item
QList<QStandardItem *> carryItems; //Last In First Out stack of items
QList<QStandardItem *> itemsToBeDeleted; //List of items to be deleted
while (loopItem->rowCount())
{
itemsToBeDeleted << loopItem->takeRow(0);
//if the row removed has children:
if (itemsToBeDeleted.at(0)->hasChildren())
{
carryItems << loopItem; //put on the stack the current loopItem
loopItem = itemsToBeDeleted.at(0); //set the row with children as the loopItem
}
//if current loopItem has no more rows but carryItems list is not empty:
if (!loopItem->rowCount() && !carryItems.isEmpty()) loopItem = carryItems.takeFirst();
}
qDeleteAll(itemsToBeDeleted);
To copy to clipboard, switch view to plain text mode
Bookmarks