[Qt-interest] Deleting the treewidget item
Srdjan Todorovic
todorovic.s at googlemail.com
Mon Jun 22 10:36:44 CEST 2009
Hi,
On 22/06/2009, Ferenc Stelcz <ferenc at stelcz.hu> wrote:
> //This will remove all the children of *item*
> //Be warned, that the child items won't be deleted!
> for(int childcount=0; i<item->childCount(); ++i)
> {
> QTreeWidgetItem *currentChildItem = item->child(i);
> item->removeChild(currentChildItem);
>
> /*
> delete currentChildItem;
> currentChildItem = 0;
> */
> }
I've been a bit wary of deleting items from a tree widget. What
happens if you have 10 children, and childcount == 9?
If you have removed the first 9 children, won't item->child(9) be out of bounds?
> //This piece tries to remove *item*
> QTreeWidgetItem *rootItem = item->parent(); //We get the parent of item
> rootItem->removeChild(item); //Then remove item
So I tend to do something like this:
QList<QTreeWidgetItem *> children = item->takeChildren();
for (int i = 0; i < children.count(); ++i)
{
QTreeWidgetItem *tmp = children.at(i);
delete tmp;
tmp = 0;
}
Which means these items are under my control now and I should be able
to delete them safely (assuming the pointers are not used somewhere
else).
Srdjan
More information about the Qt-interest-old
mailing list