[Qt-interest] Deleting the treewidget item
Ferenc Stelcz
ferenc at stelcz.hu
Mon Jun 22 12:12:28 CEST 2009
Srdjan Todorovic wrote:
>
> 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
Yes, that's why I have replied to myself with the take*() methods.
Haven't got enough coffee in the morning... :)
Thank you!
--
Ferenc Stelcz
Junior Software Engineer
Banyan Technologies LLC.
__________ Information from ESET Smart Security, version of virus signature database 4177 (20090622) __________
The message was checked by ESET Smart Security.
http://www.eset.com
More information about the Qt-interest-old
mailing list