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. How to release memory in QTreeWidget.

How to release memory in QTreeWidget.

Scheduled Pinned Locked Moved Unsolved General and Desktop
qtreewidgetqtreewidgetitem
7 Posts 4 Posters 2.2k Views
  • 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.
  • rohit713R Offline
    rohit713R Offline
    rohit713
    wrote on last edited by VRonin
    #1

    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.

    jsulmJ 1 Reply Last reply
    0
    • rohit713R rohit713

      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.

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @rohit713 You should call deleteLater() on the item you want to free.
      Also you should never create/modify any UI related class instances in other thread than GUI thread!

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      2
      • rohit713R Offline
        rohit713R Offline
        rohit713
        wrote on last edited by VRonin
        #3

        hii @jsulm thanks for responding. I am not updating gui from QThread. I just emit a signal from QThread when I search the item. Then this signal is connected to QMainWindow slots for adding items in QTreeWidget using QTreeWidgetItem *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 by QTreeWidgetItem. How can I use deleteLater().

        jsulmJ 1 Reply Last reply
        0
        • rohit713R rohit713

          hii @jsulm thanks for responding. I am not updating gui from QThread. I just emit a signal from QThread when I search the item. Then this signal is connected to QMainWindow slots for adding items in QTreeWidget using QTreeWidgetItem *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 by QTreeWidgetItem. How can I use deleteLater().

          jsulmJ Offline
          jsulmJ Offline
          jsulm
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @rohit713

          while(*itr)
          {
           (*itr)->deleteLater();
          ++itr
          }

          https://forum.qt.io/topic/113070/qt-code-of-conduct

          rohit713R 1 Reply Last reply
          0
          • jsulmJ jsulm

            @rohit713

            while(*itr)
            {
             (*itr)->deleteLater();
            ++itr
            }
            rohit713R Offline
            rohit713R Offline
            rohit713
            wrote on last edited by
            #5

            @jsulm I tried this. But I got an error. class QTreeWidgetItem has no member named 'deleteLater' .

            1 Reply Last reply
            0
            • N Offline
              N Offline
              narunlifescience
              wrote on last edited by
              #6

              you are only deleting the root item and leaving its children untouched
              I would suggest

                while (treeWidget->topLevelItemCount()) {
                  QTreeWidgetItemIterator itr(treeWidget,
                                              QTreeWidgetItemIterator::NoChildren);
                  while (*itr) {
                    delete (*itr);
                    ++itr;
                  }
                }
              
              1 Reply Last reply
              0
              • VRoninV Offline
                VRoninV Offline
                VRonin
                wrote on last edited by VRonin
                #7

                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

                "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                ~Napoleon Bonaparte

                On a crusade to banish setIndexWidget() from the holy land of Qt

                1 Reply Last reply
                2

                • Login

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