Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QTreeWidget: how to remove all items with widgets correctly?
Forum Updated to NodeBB v4.3 + New Features

QTreeWidget: how to remove all items with widgets correctly?

Scheduled Pinned Locked Moved Unsolved General and Desktop
12 Posts 4 Posters 2.3k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Christian EhrlicherC Offline
    Christian EhrlicherC Offline
    Christian Ehrlicher
    Lifetime Qt Champion
    wrote on last edited by
    #2

    ui->treeWidget->clear(); is enough as QTreeWidget takes ownership of all the QTreeWidgetItems added to the QTreeWidget.

    Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
    Visit the Qt Academy at https://academy.qt.io/catalog

    sitesvS 1 Reply Last reply
    2
    • Christian EhrlicherC Christian Ehrlicher

      ui->treeWidget->clear(); is enough as QTreeWidget takes ownership of all the QTreeWidgetItems added to the QTreeWidget.

      sitesvS Offline
      sitesvS Offline
      sitesv
      wrote on last edited by
      #3

      @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?

      JonBJ 1 Reply Last reply
      0
      • sitesvS sitesv

        @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?

        JonBJ Offline
        JonBJ Offline
        JonB
        wrote on last edited by
        #4

        @sitesv

        But what about qwitgets that were added to the QTreeWidgetItems?

        Via QTreeWidget::setItemWidget()?

        Note: The tree takes ownership of the widget.

        sitesvS 1 Reply Last reply
        0
        • JonBJ JonB

          @sitesv

          But what about qwitgets that were added to the QTreeWidgetItems?

          Via QTreeWidget::setItemWidget()?

          Note: The tree takes ownership of the widget.

          sitesvS Offline
          sitesvS Offline
          sitesv
          wrote on last edited by
          #5

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

          JonBJ 1 Reply Last reply
          0
          • sitesvS sitesv

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

            JonBJ Offline
            JonBJ Offline
            JonB
            wrote on last edited by
            #6

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

            1 Reply Last reply
            2
            • sitesvS Offline
              sitesvS Offline
              sitesv
              wrote on last edited by
              #7

              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?

              1 Reply Last reply
              0
              • nageshN Offline
                nageshN Offline
                nagesh
                wrote on last edited by
                #8

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

                sitesvS 1 Reply Last reply
                1
                • nageshN nagesh

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

                  sitesvS Offline
                  sitesvS Offline
                  sitesv
                  wrote on last edited by
                  #9

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

                  1 Reply Last reply
                  0
                  • sitesvS Offline
                    sitesvS Offline
                    sitesv
                    wrote on last edited by sitesv
                    #10

                    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();
                        }
                    }
                    
                    JonBJ 1 Reply Last reply
                    0
                    • sitesvS sitesv

                      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();
                          }
                      }
                      
                      JonBJ Offline
                      JonBJ Offline
                      JonB
                      wrote on last edited by JonB
                      #11

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

                      sitesvS 1 Reply Last reply
                      0
                      • JonBJ JonB

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

                        sitesvS Offline
                        sitesvS Offline
                        sitesv
                        wrote on last edited by sitesv
                        #12

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

                        1 Reply Last reply
                        0

                        • Login

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • Users
                        • Groups
                        • Search
                        • Get Qt Extensions
                        • Unsolved