QTreeWidget: how to remove all items with widgets correctly?
-
Hi!
I trying to test the creation and removing of QTreeWidget items (each item contains widgets) in showEvent() method (infinity mode (while(1))...
I see a memory leak in the dispatcher..for removing items I using next code:
for(int i = 0; i < ui->treeWidget->topLevelItemCount(); i++){ qDeleteAll(ui->treeWidget->topLevelItem(i)->takeChildren()); } ui->treeWidget->clear();
Should I delete widgets manually? or something another...
-
ui->treeWidget->clear(); is enough as QTreeWidget takes ownership of all the QTreeWidgetItems added to the QTreeWidget.
-
@Christian-Ehrlicher said in QTreeWidget: how to remove all items with widgets correctly?:
ui->treeWidget->clear(); is enough as QTreeWidget takes ownership of all the QTreeWidgetItems added to the QTreeWidget.
Hi!
But what about qwitgets that were added to the QTreeWidgetItems? -
@JonB said in QTreeWidget: how to remove all items with widgets correctly?:
But what about qwitgets that were added to the QTreeWidgetItems?
Via QTreeWidget::setItemWidget()?
Note: The tree takes ownership of the widget.
Yes!
But I don't know what is the cause of the memory leak... -
I see a memory leak in the dispatcher..
I don't know what "the dispatcher" is. Nor why you suspect this tree widget code. If you are using Linux use
valgrind
to track down memory leaks. If you are using Windows, say so! Though then I'm not sure what you're supposed to use.in showEvent() method (infinity mode (while(1))...
I don't know what this mean either. But if you doing something continuously, e.g. without letting the Qt event loop run and potentially release freed objects, you may be getting a "false" reading, i.e. using more and more memory may be expected.
-
Hi!
Simple example. Host: Linux.void MainWindow::on_pushButton_clicked() { while(1) { ui->treeWidget->setColumnCount(2); connect(ui->treeWidget, &QTreeWidget::itemClicked, this, &MainWindow::treewidget_item_clicked); ui->treeWidget->clear(); QApplication::processEvents(); } }
This is my simple example of a memory leak. If I delete the 'connect' row - there is no more leak.
I see that in the system monitor.
What I do wrong? -
-
Made some update... This example leaks a large amount of memory.
Is this cause by the next notation?
About setItemWidget:
This function should only be used to display static content in the place of a tree widget item...void MainWindow::on_pushButton_clicked() { ui->treeWidget->setColumnCount(2); connect(ui->treeWidget, &QTreeWidget::itemClicked, this, &MainWindow::treewidget_item_clicked); ui->treeWidget->setSelectionMode(QAbstractItemView::SingleSelection); while(1) { for(int i = 0; i < 5; i++){ QTreeWidgetItem *itm = new QTreeWidgetItem; itm->setText(0, "text"); ui->treeWidget->addTopLevelItem(itm); QComboBox *w = new QComboBox; w->setObjectName(QString("my_object_%1").arg(i)); ui->treeWidget->setItemWidget(itm, 1, w); } ui->treeWidget->expandAll(); ui->treeWidget->clear(); QApplication::processEvents(); } }
-
while(1) { ... QApplication::processEvents(); }
I would not test for memory leaks with this, and I would not write code like this. If I were you I would change over to, say, a
QTimer
to run the body of your loop on an interval, and see how that compares.