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.
  • R Offline
    R Offline
    rohit713
    wrote on 13 Feb 2018, 11:18 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.

    J 1 Reply Last reply 13 Feb 2018, 11:24
    0
    • R rohit713
      13 Feb 2018, 11:18

      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.

      J Offline
      J Offline
      jsulm
      Lifetime Qt Champion
      wrote on 13 Feb 2018, 11:24 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
      • R Offline
        R Offline
        rohit713
        wrote on 13 Feb 2018, 11:39 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().

        J 1 Reply Last reply 13 Feb 2018, 11:52
        0
        • R rohit713
          13 Feb 2018, 11:39

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

          J Offline
          J Offline
          jsulm
          Lifetime Qt Champion
          wrote on 13 Feb 2018, 11:52 last edited by
          #4

          @rohit713

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

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

          R 1 Reply Last reply 14 Feb 2018, 03:24
          0
          • J jsulm
            13 Feb 2018, 11:52

            @rohit713

            while(*itr)
            {
             (*itr)->deleteLater();
            ++itr
            }
            R Offline
            R Offline
            rohit713
            wrote on 14 Feb 2018, 03:24 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 4 Dec 2018, 14:18 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
              • V Offline
                V Offline
                VRonin
                wrote on 4 Dec 2018, 17:13 last edited by VRonin 12 Apr 2018, 17:14
                #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