Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

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...


  • Lifetime Qt Champion

    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?



  • @sitesv

    But what about qwitgets that were added to the QTreeWidgetItems?

    Via QTreeWidget::setItemWidget()?

    Note: The tree takes ownership of the widget.



  • @JonB said in QTreeWidget: how to remove all items with widgets correctly?:

    @sitesv

    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...



  • @sitesv

    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?



  • @sitesv btw what does this MainWindow::on_pushButton_clicked() trying to achieve in while loop?
    When do you terminate the loop?
    Try connect keeping outside while loop..



  • @nagesh said in QTreeWidget: how to remove all items with widgets correctly?:

    @sitesv btw what does this MainWindow::on_pushButton_clicked() trying to achieve in while loop?

    I cut a lot of code. I make a simple memory leak test by while(1)...



  • 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();
        }
    }
    


  • @sitesv

        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.



  • @JonB the same result. Memory leak increases with 100 KB/s rate...
    UPD: Hm.. 'Leak' has stoped on 8MB....


Log in to reply