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 do I delete the current QTreeWidget element correctly?
Forum Updated to NodeBB v4.3 + New Features

How do I delete the current QTreeWidget element correctly?

Scheduled Pinned Locked Moved Solved General and Desktop
33 Posts 4 Posters 4.6k 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.
  • H Helg1980

    Crashes when trying to delete Parent row

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

    @Helg1980
    It's not clear what you mean. @VRonin's code does not try to delete a parent, so what "Parent row"? "Crashes" could mean many things, do you compile for debug and run your code in the debugger?

    1 Reply Last reply
    0
    • H Offline
      H Offline
      Helg1980
      wrote on last edited by
      #5

      Here's what works for me:

      void SelectPreset::on_actionRemove_preset_clicked()  // Remove preset
      {
          int index = ui_selectpreset->treeWidget->currentIndex().row();
          if (index < 0) {
              std::cout << "Negative index..." << std::endl;
              return;
          };
          QTreeWidgetItem *item = ui_selectpreset->treeWidget->currentItem();
          bool par = item->parent();
          if (par == true) {
              std::cout << "Item is child... " << std::endl;
              QMessageBox msgBox;
              msgBox.setStyleSheet("background-color: rgb(5, 30, 35);");
              msgBox.setIcon(QMessageBox::Question);
              msgBox.setWindowTitle("Preset");
              msgBox.setText("Delete?");
              msgBox.setStandardButtons(QMessageBox::Yes | QMessageBox::Cancel);
              msgBox.setDefaultButton(QMessageBox::Cancel);
              int confirm = msgBox.exec();
              if (confirm == QMessageBox::Yes) {
                  item->parent()->removeChild(item);
              };
          } else {
              std::cout << "Item is parent... " << std::endl;
              int count_child = item->childCount();
              if (count_child == 0) {
                  ui_selectpreset->treeWidget->takeTopLevelItem(index);
              };
          };
      }
      
      JonBJ 1 Reply Last reply
      0
      • H Helg1980

        Here's what works for me:

        void SelectPreset::on_actionRemove_preset_clicked()  // Remove preset
        {
            int index = ui_selectpreset->treeWidget->currentIndex().row();
            if (index < 0) {
                std::cout << "Negative index..." << std::endl;
                return;
            };
            QTreeWidgetItem *item = ui_selectpreset->treeWidget->currentItem();
            bool par = item->parent();
            if (par == true) {
                std::cout << "Item is child... " << std::endl;
                QMessageBox msgBox;
                msgBox.setStyleSheet("background-color: rgb(5, 30, 35);");
                msgBox.setIcon(QMessageBox::Question);
                msgBox.setWindowTitle("Preset");
                msgBox.setText("Delete?");
                msgBox.setStandardButtons(QMessageBox::Yes | QMessageBox::Cancel);
                msgBox.setDefaultButton(QMessageBox::Cancel);
                int confirm = msgBox.exec();
                if (confirm == QMessageBox::Yes) {
                    item->parent()->removeChild(item);
                };
            } else {
                std::cout << "Item is parent... " << std::endl;
                int count_child = item->childCount();
                if (count_child == 0) {
                    ui_selectpreset->treeWidget->takeTopLevelItem(index);
                };
            };
        }
        
        JonBJ Offline
        JonBJ Offline
        JonB
        wrote on last edited by
        #6

        @Helg1980
        You code may "work" for you, but you are doing nothing about your question title, "How do I delete the current QTreeWidget element correctly".

        item->parent()->removeChild(item);
        

        This *leaks":

        Removes the given item indicated by child. The removed item will not be deleted.

        ui_selectpreset->treeWidget->takeTopLevelItem(index);
        

        Removes the top-level item at the given index in the tree and returns it, otherwise returns nullptr;

        Although the docs are not explicit, I imagine this leaks in a similar fashion.

        Run your program under, say, valgrind and see what you get.

        On a separate note, it might be just me but I have never seen people write

        bool par = item->parent();
        

        assigning a pointer to a bool without any explicit cast. Do you not find, say, bool par = item->parent() != nullptr; cleaner/nicer?

        1 Reply Last reply
        0
        • H Offline
          H Offline
          Helg1980
          wrote on last edited by
          #7

          Here's what I got after the adjustments:

          void SelectPreset::on_actionRemove_preset_clicked()  // Remove preset
          {
              int index = ui_selectpreset->treeWidget->currentIndex().row();
              if (index < 0) {
                  std::cout << "Negative index..." << std::endl;
                  return;
              };
              QTreeWidgetItem *item = ui_selectpreset->treeWidget->currentItem();
              QTreeWidgetItem *parentItem = item->parent();
              //Q_ASSERT(parentItem);
              if (parentItem != nullptr) {
                  std::cout << "Item is child... " << std::endl;
                  QMessageBox msgBox;
                  msgBox.setStyleSheet("background-color: rgb(5, 30, 35);");
                  msgBox.setIcon(QMessageBox::Question);
                  msgBox.setWindowTitle("Preset");
                  msgBox.setText("Delete?");
                  msgBox.setStandardButtons(QMessageBox::Yes | QMessageBox::Cancel);
                  msgBox.setDefaultButton(QMessageBox::Cancel);
                  int confirm = msgBox.exec();
                  if (confirm == QMessageBox::Yes) {
                      QTreeWidgetItem *takenItem = parentItem->takeChild(parentItem->indexOfChild(item));
                      //Q_ASSERT(takenItem==item);
                      delete takenItem;
                  };
              } else {
                  std::cout << "Item is parent... " << std::endl;
                  std::cout << "Index top level: " << index << std::endl;
                  int count_child = item->childCount();
                  if (count_child == 0) {
                      ui_selectpreset->treeWidget->takeTopLevelItem(index);
                  } else {
                      QMessageBox msgBox;
                      msgBox.setStyleSheet("background-color: rgb(5, 30, 35);");
                      msgBox.setIcon(QMessageBox::Information);
                      msgBox.setWindowTitle("Preset");
                      msgBox.setText("Delete presets first! ");
                      msgBox.setStandardButtons(QMessageBox::Ok);
                      msgBox.exec();
                  };
              };
          }
          
          JonBJ 1 Reply Last reply
          0
          • H Helg1980

            Here's what I got after the adjustments:

            void SelectPreset::on_actionRemove_preset_clicked()  // Remove preset
            {
                int index = ui_selectpreset->treeWidget->currentIndex().row();
                if (index < 0) {
                    std::cout << "Negative index..." << std::endl;
                    return;
                };
                QTreeWidgetItem *item = ui_selectpreset->treeWidget->currentItem();
                QTreeWidgetItem *parentItem = item->parent();
                //Q_ASSERT(parentItem);
                if (parentItem != nullptr) {
                    std::cout << "Item is child... " << std::endl;
                    QMessageBox msgBox;
                    msgBox.setStyleSheet("background-color: rgb(5, 30, 35);");
                    msgBox.setIcon(QMessageBox::Question);
                    msgBox.setWindowTitle("Preset");
                    msgBox.setText("Delete?");
                    msgBox.setStandardButtons(QMessageBox::Yes | QMessageBox::Cancel);
                    msgBox.setDefaultButton(QMessageBox::Cancel);
                    int confirm = msgBox.exec();
                    if (confirm == QMessageBox::Yes) {
                        QTreeWidgetItem *takenItem = parentItem->takeChild(parentItem->indexOfChild(item));
                        //Q_ASSERT(takenItem==item);
                        delete takenItem;
                    };
                } else {
                    std::cout << "Item is parent... " << std::endl;
                    std::cout << "Index top level: " << index << std::endl;
                    int count_child = item->childCount();
                    if (count_child == 0) {
                        ui_selectpreset->treeWidget->takeTopLevelItem(index);
                    } else {
                        QMessageBox msgBox;
                        msgBox.setStyleSheet("background-color: rgb(5, 30, 35);");
                        msgBox.setIcon(QMessageBox::Information);
                        msgBox.setWindowTitle("Preset");
                        msgBox.setText("Delete presets first! ");
                        msgBox.setStandardButtons(QMessageBox::Ok);
                        msgBox.exec();
                    };
                };
            }
            
            JonBJ Offline
            JonBJ Offline
            JonB
            wrote on last edited by JonB
            #8

            @Helg1980 said in How do I delete the current QTreeWidget element correctly?:

            delete takenItem;

            That is better.

            //Q_ASSERT(takenItem==item);

            I would have thought that should hold, is there a reason you commented out?

            ui_selectpreset->treeWidget->takeTopLevelItem(index);

            I still think that needs delete-ing? Check with valgrind or other memory leak checker?

            1 Reply Last reply
            1
            • H Offline
              H Offline
              Helg1980
              wrote on last edited by
              #9

              @Helg1980 said in How do I delete the current QTreeWidget element correctly?:

              //Q_ASSERT(parentItem);

              Q_ASSERT(parentItem);
              

              For some reason, I have a crash with this command, and there is no decryption of the ASSERT error, despite the fact that I added #include <QtGlobal>, so I commented out all the lines with Q_ASSERT.

              Please tell me an alternative to the command

              ui_selectpreset->treeWidget->takeTopLevelItem(index);
              

              to avoid leakage.

              JonBJ 1 Reply Last reply
              0
              • H Helg1980

                @Helg1980 said in How do I delete the current QTreeWidget element correctly?:

                //Q_ASSERT(parentItem);

                Q_ASSERT(parentItem);
                

                For some reason, I have a crash with this command, and there is no decryption of the ASSERT error, despite the fact that I added #include <QtGlobal>, so I commented out all the lines with Q_ASSERT.

                Please tell me an alternative to the command

                ui_selectpreset->treeWidget->takeTopLevelItem(index);
                

                to avoid leakage.

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

                @Helg1980

                Q_ASSERT(parentItem);

                For some reason, I have a crash with this command, and there is no decryption of the ASSERT error

                When compiled for debug, failure on this line should simply show you which line (probably including the word parentItem) it failed on. That's what Q_ASSERT() does.

                Q_ASSERT(parentItem); in your code will correctly assert, when the item being deleted has no parent (i.e. top-level). Therefore your code should indeed not have that line. But the line I said it could have is the commented-out //Q_ASSERT(takenItem==item);, which should succeed logic-wise. This is a detail.

                Please tell me an alternative to the command

                ui_selectpreset->treeWidget->takeTopLevelItem(index);

                to avoid leakage.

                You wouldn't want me to tell you, because you know this. When deleting a non-top-level element you already have

                            QTreeWidgetItem *takenItem = parentItem->takeChild(parentItem->indexOfChild(item));
                            Q_ASSERT(takenItem==item);
                            delete takenItem;
                

                So what do you think you have correspondingly in the takeTopLevelItem() place?

                1 Reply Last reply
                1
                • H Offline
                  H Offline
                  Helg1980
                  wrote on last edited by Helg1980
                  #11

                  I replaced the line by analogy

                  ui_selectpreset->treeWidget->takeTopLevelItem(index);
                  

                  with the following lines:

                  QTreeWidgetItem *takenItem = ui_selectpreset->treeWidget->takeTopLevelItem(ui_selectpreset->treeWidget->indexOfTopLevelItem(item));
                  Q_ASSERT(takenItem==item);
                  delete takenItem;
                  

                  (The only thing is that I couldn't start Q_ASSERT, I already turned everything on and still it doesn't work for me.)

                  Screenshot_20201024_154054.png

                  JonBJ 1 Reply Last reply
                  0
                  • H Helg1980

                    I replaced the line by analogy

                    ui_selectpreset->treeWidget->takeTopLevelItem(index);
                    

                    with the following lines:

                    QTreeWidgetItem *takenItem = ui_selectpreset->treeWidget->takeTopLevelItem(ui_selectpreset->treeWidget->indexOfTopLevelItem(item));
                    Q_ASSERT(takenItem==item);
                    delete takenItem;
                    

                    (The only thing is that I couldn't start Q_ASSERT, I already turned everything on and still it doesn't work for me.)

                    Screenshot_20201024_154054.png

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

                    @Helg1980 said in How do I delete the current QTreeWidget element correctly?:

                    The only thing is that I couldn't start Q_ASSERT

                    I have no idea what you mean by this. What does "start" mean here? Q_ASSERT() is a macro, producing code during compilation.

                    and still it doesn't work for me.

                    In what way "work" or "doesn't work"?

                    BTW, your Build Settings are not relevant to this.

                    1 Reply Last reply
                    0
                    • H Offline
                      H Offline
                      Helg1980
                      wrote on last edited by Helg1980
                      #13

                      I mean, Q_ASSERT does not output a message in which line the crash occurred (in the case of Q_ASSERT(parentItem);)
                      Therefore, I concluded that this command does not work for me..

                      JonBJ 1 Reply Last reply
                      0
                      • H Helg1980

                        I mean, Q_ASSERT does not output a message in which line the crash occurred (in the case of Q_ASSERT(parentItem);)
                        Therefore, I concluded that this command does not work for me..

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

                        @Helg1980
                        Are you compiling for Debug or Release? Q_ASSERT() does nothing when compiled for Release, only for Debug.

                        1 Reply Last reply
                        0
                        • H Offline
                          H Offline
                          Helg1980
                          wrote on last edited by
                          #15

                          Compilation I have in Debud mode, here is my compilation command:

                          cine_encoder.pro -spec linux-g++ CONFIG+=debug CONFIG+=force_debug_info CONFIG+=separate_debug_info && /usr/bin/make qmake_all
                          
                          JonBJ 1 Reply Last reply
                          0
                          • H Helg1980

                            Compilation I have in Debud mode, here is my compilation command:

                            cine_encoder.pro -spec linux-g++ CONFIG+=debug CONFIG+=force_debug_info CONFIG+=separate_debug_info && /usr/bin/make qmake_all
                            
                            JonBJ Offline
                            JonBJ Offline
                            JonB
                            wrote on last edited by JonB
                            #16

                            @Helg1980
                            That's not actually any kind of compiler command, just a make-type one, but never mind.

                            I really don't know. If the first line of your program is a Q_ASSERT(false) it should produce an error (when run) if compiled for Debug. And if you run your application from the debugger it should cause that to break on the offending line. If it's Q_ASSERT(true) it should do precisely nothing. You might test with these two.

                            P.S.
                            Where are you looking for the message to appear? If running from Qt Creator I think it would go to the Application Output pane.

                            H 1 Reply Last reply
                            0
                            • H Offline
                              H Offline
                              Helg1980
                              wrote on last edited by
                              #17

                              @Helg1980 said in How do I delete the current QTreeWidget element correctly?:

                              Q_ASSERT(parentItem);

                              That's the point, that in Q_ASSERT(parentItem), the value 'parrentItem' is false, but Q_ASSERT does not give a message about this, but simply crashes the program;

                              1 Reply Last reply
                              0
                              • JonBJ JonB

                                @Helg1980
                                That's not actually any kind of compiler command, just a make-type one, but never mind.

                                I really don't know. If the first line of your program is a Q_ASSERT(false) it should produce an error (when run) if compiled for Debug. And if you run your application from the debugger it should cause that to break on the offending line. If it's Q_ASSERT(true) it should do precisely nothing. You might test with these two.

                                P.S.
                                Where are you looking for the message to appear? If running from Qt Creator I think it would go to the Application Output pane.

                                H Offline
                                H Offline
                                Helg1980
                                wrote on last edited by
                                #18

                                Yes, I work in QtCreator.
                                Here is my Application Output:

                                18:31:38: Starting /run/media/helg/GOODRAM-SDB/QtCreatorProjects/CineEncoder/build-cine_encoder-Desktop-Debug/cine_encoder ...
                                18:31:53: The program has unexpectedly finished.
                                18:31:53: The process was ended forcefully.
                                18:31:53: /run/media/helg/GOODRAM-SDB/QtCreatorProjects/CineEncoder/build-cine_encoder-Desktop-Debug/cine_encoder crashed.
                                
                                JonBJ VRoninV 2 Replies Last reply
                                0
                                • H Helg1980

                                  Yes, I work in QtCreator.
                                  Here is my Application Output:

                                  18:31:38: Starting /run/media/helg/GOODRAM-SDB/QtCreatorProjects/CineEncoder/build-cine_encoder-Desktop-Debug/cine_encoder ...
                                  18:31:53: The program has unexpectedly finished.
                                  18:31:53: The process was ended forcefully.
                                  18:31:53: /run/media/helg/GOODRAM-SDB/QtCreatorProjects/CineEncoder/build-cine_encoder-Desktop-Debug/cine_encoder crashed.
                                  
                                  JonBJ Offline
                                  JonBJ Offline
                                  JonB
                                  wrote on last edited by
                                  #19

                                  @Helg1980
                                  OK, I don't know what you are doing, and what your program is doing.

                                  It is time now to use the debugger to put a breakpoint in your program, and step through offending code. Besides, I don't know how you are getting a "crashed" without the debugger catching it and showing you a stack trace back to the faulting line. If you are not familiar with using the debugger it's time you were, as you can resolve these issues very simply by using it!

                                  1 Reply Last reply
                                  0
                                  • H Helg1980

                                    Yes, I work in QtCreator.
                                    Here is my Application Output:

                                    18:31:38: Starting /run/media/helg/GOODRAM-SDB/QtCreatorProjects/CineEncoder/build-cine_encoder-Desktop-Debug/cine_encoder ...
                                    18:31:53: The program has unexpectedly finished.
                                    18:31:53: The process was ended forcefully.
                                    18:31:53: /run/media/helg/GOODRAM-SDB/QtCreatorProjects/CineEncoder/build-cine_encoder-Desktop-Debug/cine_encoder crashed.
                                    
                                    VRoninV Offline
                                    VRoninV Offline
                                    VRonin
                                    wrote on last edited by VRonin
                                    #20

                                    @Helg1980
                                    Start the debugger. If you have a debugger in the background it will tell you where the assertion occurred.

                                    Q_ASSERT and Q_ASSUME are used to test implicit assumptions you make in your code.

                                    Looking at the sources QTreeWidget it looked like item->parent() for a top level item would return the invisible root item. I tested this implicit assumption with Q_ASSERT and it turns out I was wrong (but at least we know why)

                                    void SelectPreset::on_actionRemove_preset_clicked()  // Remove preset
                                    {
                                        QTreeWidgetItem *item = ui_selectpreset->treeWidget->currentItem();
                                        if(!item)
                                            return;
                                        QTreeWidgetItem *parentItem = item->parent();
                                        QTreeWidgetItem *takenItem = nullptr;
                                        if(parentItem)
                                            takenItem  = parentItem->takeChild(parentItem->indexOfChild(item));
                                        else
                                            takenItem  = ui_selectpreset->treeWidget->takeTopLevelItem(ui_selectpreset->treeWidget->indexOfTopLevelItem(item));
                                        Q_ASSERT(takenItem==item);
                                        delete takenItem;
                                    }
                                    

                                    "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
                                    1
                                    • H Offline
                                      H Offline
                                      Helg1980
                                      wrote on last edited by
                                      #21

                                      Finally, I finished the program and ran it through 'valgrind'. The result was disappointing:

                                      valgrind --leak-check=full --leak-resolution=med /run/media/helg/GOODRAM-SDB/QtCreatorProjects/CineEncoder/build-cine_encoder-Desktop-Debug/cine_encoder
                                      .
                                      .
                                      .
                                      ==2615== HEAP SUMMARY:
                                      ==2615==     in use at exit: 568,907 bytes in 7,703 blocks
                                      ==2615==   total heap usage: 880,287 allocs, 872,584 frees, 5,593,630,039 bytes allocated
                                      ==2615== 
                                      ==2615== 24 bytes in 1 blocks are definitely lost in loss record 89 of 557
                                      ==2615==    at 0x483A77F: malloc (vg_replace_malloc.c:307)
                                      ==2615==    by 0xDE61E5E: XextCreateExtension (in /usr/lib/libXext.so.6.4.0)
                                      ==2615==    by 0x1074014B: ???
                                      ==2615==    by 0x1073775C: ???
                                      ==2615==    by 0x10662091: ??? (in /usr/lib/qt/plugins/xcbglintegrations/libqxcb-glx-integration.so)
                                      ==2615==    by 0xA28C084: QXcbWindow::create() (in /usr/lib/libQt5XcbQpa.so.5.15.1)
                                      ==2615==    by 0xA27A746: QXcbIntegration::createPlatformWindow(QWindow*) const (in /usr/lib/libQt5XcbQpa.so.5.15.1)
                                      ==2615==    by 0x5842F26: QWindowPrivate::create(bool, unsigned long long) (in /usr/lib/libQt5Gui.so.5.15.1)
                                      ==2615==    by 0x51F78A7: QWidgetPrivate::create() (in /usr/lib/libQt5Widgets.so.5.15.1)
                                      ==2615==    by 0x51F7E20: QWidget::create(unsigned long long, bool, bool) (in /usr/lib/libQt5Widgets.so.5.15.1)
                                      ==2615==    by 0x520551B: QWidgetPrivate::setVisible(bool) (in /usr/lib/libQt5Widgets.so.5.15.1)
                                      ==2615==    by 0x117E3F: main (main.cpp:10)
                                      ==2615== 
                                      ==2615== 96 (64 direct, 32 indirect) bytes in 1 blocks are definitely lost in loss record 316 of 557
                                      ==2615==    at 0x483ADEF: operator new(unsigned long) (vg_replace_malloc.c:342)
                                      ==2615==    by 0x159CCB: SelectPreset::SelectPreset(QWidget*) (selectpreset.cpp:28)
                                      ==2615==    by 0x129258: MainWindow::on_actionPreset_clicked() (mainwindow.cpp:1279)
                                      ==2615==    by 0x1659B3: MainWindow::qt_static_metacall(QObject*, QMetaObject::Call, int, void**) (moc_mainwindow.cpp:156)
                                      ==2615==    by 0x165B5F: MainWindow::qt_metacall(QMetaObject::Call, int, void**) (moc_mainwindow.cpp:204)
                                      ==2615==    by 0x60B5D71: ??? (in /usr/lib/libQt5Core.so.5.15.1)
                                      ==2615==    by 0x52B5B22: QAbstractButton::clicked(bool) (in /usr/lib/libQt5Widgets.so.5.15.1)
                                      ==2615==    by 0x52B64AB: ??? (in /usr/lib/libQt5Widgets.so.5.15.1)
                                      ==2615==    by 0x52B7E52: ??? (in /usr/lib/libQt5Widgets.so.5.15.1)
                                      ==2615==    by 0x52B8032: QAbstractButton::mouseReleaseEvent(QMouseEvent*) (in /usr/lib/libQt5Widgets.so.5.15.1)
                                      ==2615==    by 0x5205B0D: QWidget::event(QEvent*) (in /usr/lib/libQt5Widgets.so.5.15.1)
                                      ==2615==    by 0x51C4751: QApplicationPrivate::notify_helper(QObject*, QEvent*) (in /usr/lib/libQt5Widgets.so.5.15.1)
                                      ==2615== 
                                      ==2615== 160 bytes in 1 blocks are possibly lost in loss record 369 of 557
                                      ==2615==    at 0x483A77F: malloc (vg_replace_malloc.c:307)
                                      ==2615==    by 0x60A8595: QObjectPrivate::addConnection(int, QObjectPrivate::Connection*) (in /usr/lib/libQt5Core.so.5.15.1)
                                      ==2615==    by 0x60AC9DE: QObjectPrivate::connectImpl(QObject const*, int, QObject const*, void**, QtPrivate::QSlotObjectBase*, Qt::ConnectionType, int const*, QMetaObject const*) (in /usr/lib/libQt5Core.so.5.15.1)
                                      ==2615==    by 0x60ACEB5: QObject::connectImpl(QObject const*, void**, QObject const*, void**, QtPrivate::QSlotObjectBase*, Qt::ConnectionType, int const*, QMetaObject const*) (in /usr/lib/libQt5Core.so.5.15.1)
                                      ==2615==    by 0xA3D1BBD: QDBusConnectionInterface::QDBusConnectionInterface(QDBusConnection const&, QObject*) (in /usr/lib/libQt5DBus.so.5.15.1)
                                      ==2615==    by 0xA3CEB3A: ??? (in /usr/lib/libQt5DBus.so.5.15.1)
                                      ==2615==    by 0xA3CFD13: ??? (in /usr/lib/libQt5DBus.so.5.15.1)
                                      ==2615==    by 0x60AB7E1: QObject::event(QEvent*) (in /usr/lib/libQt5Core.so.5.15.1)
                                      ==2615==    by 0x607ECAF: QCoreApplication::notifyInternal2(QObject*, QEvent*) (in /usr/lib/libQt5Core.so.5.15.1)
                                      ==2615==    by 0x60817D2: QCoreApplicationPrivate::sendPostedEvents(QObject*, int, QThreadData*) (in /usr/lib/libQt5Core.so.5.15.1)
                                      ==2615==    by 0x60D8303: ??? (in /usr/lib/libQt5Core.so.5.15.1)
                                      ==2615==    by 0x6A2CBFB: g_main_context_dispatch (in /usr/lib/libglib-2.0.so.0.6600.0)
                                      ==2615== 
                                      ==2615== 302 (256 direct, 46 indirect) bytes in 1 blocks are definitely lost in loss record 409 of 557
                                      ==2615==    at 0x483A77F: malloc (vg_replace_malloc.c:307)
                                      ==2615==    by 0xA38F9E2: ??? (in /usr/lib/libfontconfig.so.1.12.0)
                                      ==2615==    by 0xA3902E4: ??? (in /usr/lib/libfontconfig.so.1.12.0)
                                      ==2615==    by 0xA391BEB: ??? (in /usr/lib/libfontconfig.so.1.12.0)
                                      ==2615==    by 0xA399E59: ??? (in /usr/lib/libfontconfig.so.1.12.0)
                                      ==2615==    by 0xA5309E7: ??? (in /usr/lib/libexpat.so.1.6.11)
                                      ==2615==    by 0xA52EDDC: ??? (in /usr/lib/libexpat.so.1.6.11)
                                      ==2615==    by 0xA532620: ??? (in /usr/lib/libexpat.so.1.6.11)
                                      ==2615==    by 0xA5348CB: XML_ParseBuffer (in /usr/lib/libexpat.so.1.6.11)
                                      ==2615==    by 0xA397024: ??? (in /usr/lib/libfontconfig.so.1.12.0)
                                      ==2615==    by 0xA397657: ??? (in /usr/lib/libfontconfig.so.1.12.0)
                                      ==2615==    by 0xA397713: ??? (in /usr/lib/libfontconfig.so.1.12.0)
                                      ==2615== 
                                      ==2615== LEAK SUMMARY:
                                      ==2615==    definitely lost: 344 bytes in 3 blocks
                                      ==2615==    indirectly lost: 78 bytes in 3 blocks
                                      ==2615==      possibly lost: 160 bytes in 1 blocks
                                      ==2615==    still reachable: 568,305 bytes in 7,694 blocks
                                      ==2615==                       of which reachable via heuristic:
                                      ==2615==                         newarray           : 4,264 bytes in 1 blocks
                                      ==2615==         suppressed: 20 bytes in 2 blocks
                                      ==2615== Reachable blocks (those to which a pointer was found) are not shown.
                                      ==2615== To see them, rerun with: --leak-check=full --show-leak-kinds=all
                                      ==2615== 
                                      ==2615== Use --track-origins=yes to see where uninitialised values come from
                                      ==2615== For lists of detected and suppressed errors, rerun with: -s
                                      ==2615== ERROR SUMMARY: 107 errors from 107 contexts (suppressed: 0 from 0)
                                      
                                      1 Reply Last reply
                                      0
                                      • Christian EhrlicherC Offline
                                        Christian EhrlicherC Offline
                                        Christian Ehrlicher
                                        Lifetime Qt Champion
                                        wrote on last edited by
                                        #22

                                        @Helg1980 said in How do I delete the current QTreeWidget element correctly?:

                                        The result was disappointing:

                                        Why? There is only one leak which should interest you:

                                        ==2615== by 0x159CCB: SelectPreset::SelectPreset(QWidget*) (selectpreset.cpp:28)

                                        The rest are no real 'leaks' and can be ignored.

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

                                        1 Reply Last reply
                                        1
                                        • H Offline
                                          H Offline
                                          Helg1980
                                          wrote on last edited by Helg1980
                                          #23

                                          Here is a part of the text where there may be an error:

                                          SelectPreset::SelectPreset(QWidget *parent) :
                                              QDialog(parent),
                                              ui_selectpreset(new Ui::SelectPreset)
                                          {
                                              ui_selectpreset->setupUi(this);
                                              ui_selectpreset->treeWidget->clear();
                                              int n =  _preset_table[0].size();
                                              int m = _preset_table.size();
                                              QString type;
                                              QColor color;
                                              color.setRgba(qRgb(120, 120, 150));
                                              QFont font;
                                              font.setBold(true);
                                              QTreeWidgetItem *item = new QTreeWidgetItem();    //    line 28  -  deleted this line
                                              for (int i = 0; i < n; i++) {
                                                  type = _preset_table[23][i];
                                                  if (type == "TopLewelItem") {
                                                      QTreeWidgetItem *root = new QTreeWidgetItem();
                                                      root->setText(0, _preset_table[0][i]);
                                                      ui_selectpreset->treeWidget->addTopLevelItem(root);
                                                      ui_selectpreset->treeWidget->setCurrentItem(root);
                                                      root->setFlags(Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsEditable);
                                                      root->setFont(0, font);
                                                      root->setForeground(0, color);
                                                  };
                                                  if (type == "ChildItem") {
                                                      item = ui_selectpreset->treeWidget->currentItem();   // replaced with: QTreeWidgetItem *item =  ....
                                                      QTreeWidgetItem *child = new QTreeWidgetItem();
                                                      for (int j = 0; j < 23; j++) {
                                                          child->setText(j, _preset_table[j][i]);
                                                      };
                                                      child->setFlags(Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsEditable);
                                                      item->addChild(child);
                                                  };
                                              };
                                              item = ui_selectpreset->treeWidget->topLevelItem(pos_top)->child(pos_cld);   // replaced with: QTreeWidgetItem *item =  ....
                                              ui_selectpreset->treeWidget->setCurrentItem(item);
                                              std::cout << n << " x " << m << std::endl; // Table size
                                              ui_selectpreset->treeWidget->expandAll();
                                              for (int i=1; i<=24; i++) {
                                                  ui_selectpreset->treeWidget->hideColumn(i);
                                              };
                                              QAction *addsection = new QAction(tr("Add section"), this);
                                              QAction *addpreset = new QAction(tr("Add new preset"), this);
                                              connect(addsection, &QAction::triggered, this, &SelectPreset::add_section);
                                              connect(addpreset, &QAction::triggered, this, &SelectPreset::add_preset);
                                              QMenu* menu = new QMenu(this);
                                              menu->addAction(addsection);
                                              menu->addAction(addpreset);
                                              menu->setStyleSheet("QMenu {background-color: rgb(5, 20, 25);} "
                                                                  "QMenu::item {background-color: transparent;} "
                                                                  "QMenu::item:selected {background-color: rgb(5, 40, 45);}");
                                              ui_selectpreset->actionAdd_preset->setMenu(menu);
                                          }
                                          
                                          JonBJ 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