2
votes

Im writing a c++ qt application. In one part I'm creating a QTreeWidget. The user has all possibilities to create and delete entries in that tree. So when the user creates a item he will call a function which then itself calls:

QTreeWidgetItem* newItem = new QTreeWidgetItem();

When the user later decides to delete the entry, he calls a function which then itself will call:

QTreeWidgetItem* curItem = _ui->qTreeWidget->currentItem();
QTreeWidgetItem* parent = curItem->parent();
parent->removeChild(curItem);

The question that erases for me is now: what about the memory this item occupied? What The Qt 4.7 doc says to removeChild is the following:

void QTreeWidgetItem::removeChild ( QTreeWidgetItem * child ) Removes the given item indicated by child. The removed item will not be deleted.

So how do i delete a child?

Thanks a lot in advance! Donny

2
@Donny: I noticed that you are new to SO (StackOverflow), so first of all, welcome on this site! Did you take a look at the faq? I'm just asking because I noticed that you did not vote up any of the answers to your question. If you find an answer useful, you should click on the little triangle pointing up on the left of the answer; this will reward the poster with "reputation" and indicate that the answer was helpful. If you are already familiar with SO customs and did not vote up on purpose, just ignore this comment :)! Anyway, have fun on SO!Luc Touraille

2 Answers

2
votes

How about

delete curItem;

?

According to the documentation, the destructor will remove the item from the tree in which it is included, so I think you don't even need to perform the removeChild beforehand.

1
votes

You would normally just delete your object at the right time with:

delete curItem;

The remove is just removing its link from the parent.

or perhaps schedule it for deletion later:

curItem->deleteLater();