How to release memory in QTreeWidget.
-
Hello I have a QTreeWidget, in which I add the items from a QThread it works well. Now If I am going to start QThread again, before it I called QTreeWidget::clear() , for clearing the tree to add new items. It works fine to clear the tree but not release the memory occupied by the QTreeWidgetItem. I tried
QTreeWidgetItemIterator itr(treeWidget); while(*itr) { delete (*itr); ++itr }
with QTreeWidget::clear();. So I want to know how can I release memory from QTreeWidget. Or How Can I properly delete QTreewidgetItem with memory release.
-
hii @jsulm thanks for responding. I am not updating gui from
QThread
. I just emit a signal fromQThread
when I search the item. Then this signal is connected toQMainWindow
slots for adding items inQTreeWidget
usingQTreeWidgetItem *item = new QTreeWidgetItem(treeWidget)
This new items called every time when thread got anything. Now the question is when I am going to start thread again. I have to release memory, consumed byQTreeWidgetItem
. How can I usedeleteLater()
. -
you are only deleting the root item and leaving its children untouched
I would suggestwhile (treeWidget->topLevelItemCount()) { QTreeWidgetItemIterator itr(treeWidget, QTreeWidgetItemIterator::NoChildren); while (*itr) { delete (*itr); ++itr; } }
-
treeWidget->model()->removeRows(0,treeWidget->model()->rowCount());
clears the widget of all items and deletes them freeing the memory (see the sources if you don't believe me). Be aware that it's difficult to assess memory usage live in debug mode as the OS might keep the memory allocated for a while after you call delete