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 delete an item in QTreeWidget?
Forum Updated to NodeBB v4.3 + New Features

How to delete an item in QTreeWidget?

Scheduled Pinned Locked Moved Solved General and Desktop
19 Posts 5 Posters 14.1k 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.
  • M MasterBlade

    @Christian-Ehrlicher said in How to delete an item in QTreeWidget?:

    You're looking for removeChild: http://doc.qt.io/qt-5/qtreewidgetitem.html#removeChild

    So I need to find his parent and then delete him as a child?

    Taz742T Offline
    Taz742T Offline
    Taz742
    wrote on last edited by
    #9

    @MasterBlade
    What if it does not have a parent?

    Do what you want.

    1 Reply Last reply
    0
    • Taz742T Taz742

      @MasterBlade said in How to delete an item in QTreeWidget?:

      First I tried delete s.constFirst(); but the program just crashed.

      First of all, why did this problem happen? Search it.
      Note: If you delete a parent all its child will be deleted. Maybe you are using this item or this childrens somewhere?

      Also are you sure

      @MasterBlade said in How to delete an item in QTreeWidget?:

      QList<QTreeWidgetItem*> s = ui->DeckList->findItems(ui->CardList->item(row, 0)->text(),
      Qt::MatchFlag::MatchExactly, 1);

      s is non empty list ?
      As documentation says http://doc.qt.io/qt-5/qlist.html#constFirst

      Returns a reference to the last item in the list. The list must not be empty. If the list can be empty, call isEmpty() before calling this function.
      
      This function was introduced in Qt 5.6.
      
      M Offline
      M Offline
      MasterBlade
      wrote on last edited by MasterBlade
      #10

      @Taz742 said in How to delete an item in QTreeWidget?:

      @MasterBlade said in How to delete an item in QTreeWidget?:

      First I tried delete s.constFirst(); but the program just crashed.

      First of all, why did this problem happen? Search it.
      Note: If you delete a parent all its child will be deleted. Maybe you are using this item or this childrens somewhere?

      I tried but found no relating result.
      Actually this is a very simple tree and no item has any children at all.
      Could you give me an example on deleting an item from searching?

      Also are you sure

      @MasterBlade said in How to delete an item in QTreeWidget?:

      QList<QTreeWidgetItem*> s = ui->DeckList->findItems(ui->CardList->item(row, 0)->text(),
      Qt::MatchFlag::MatchExactly, 1);

      s is non empty list ?
      As documentation says http://doc.qt.io/qt-5/qlist.html#constFirst

      Returns a reference to the last item in the list. The list must not be empty. If the list can be empty, call isEmpty() before calling this function.
      
      This function was introduced in Qt 5.6.
      

      Well actually I tested if s is empty or not. But as it's not important here I just delete it on this forum to make things more clear. The whole function looks like this.

      I want to deduct the number(a value) of the item by one. If the number is already 1 before calling this function, remove it instead.

      void Deck::RMD(int row)
      {
          QList<QTreeWidgetItem*> s = ui->DeckList->findItems(ui->CardList->item(row, 0)->text(),
                                                              Qt::MatchFlag::MatchExactly, 1);
          if (s.isEmpty()){
              qDebug("Can't find the card on Deck::RMD");
              return;
          }
          else{
              uint8_t no = s.constFirst()->text(0).toUInt();
              if (no == 1){
                  QTreeWidgetItem *item = s.constFirst();
                  QTreeWidgetItem *parent= item->parent();
                  uint16_t row = ui->DeckList->indexFromItem(item, 1).row();
                  ui->DeckList->topLevelItem(row);
                  delete item;
                  item = NULL;
                  if (parent != NULL && parent->childCount() == 0){
                      delete parent;
                      parent = NULL;
                  }
              }
              else{
                  s.constFirst()->setText(0, QString::number(no - 1));
              }
          }
      }
      
      Taz742T 1 Reply Last reply
      0
      • M MasterBlade

        @Taz742 said in How to delete an item in QTreeWidget?:

        @MasterBlade said in How to delete an item in QTreeWidget?:

        First I tried delete s.constFirst(); but the program just crashed.

        First of all, why did this problem happen? Search it.
        Note: If you delete a parent all its child will be deleted. Maybe you are using this item or this childrens somewhere?

        I tried but found no relating result.
        Actually this is a very simple tree and no item has any children at all.
        Could you give me an example on deleting an item from searching?

        Also are you sure

        @MasterBlade said in How to delete an item in QTreeWidget?:

        QList<QTreeWidgetItem*> s = ui->DeckList->findItems(ui->CardList->item(row, 0)->text(),
        Qt::MatchFlag::MatchExactly, 1);

        s is non empty list ?
        As documentation says http://doc.qt.io/qt-5/qlist.html#constFirst

        Returns a reference to the last item in the list. The list must not be empty. If the list can be empty, call isEmpty() before calling this function.
        
        This function was introduced in Qt 5.6.
        

        Well actually I tested if s is empty or not. But as it's not important here I just delete it on this forum to make things more clear. The whole function looks like this.

        I want to deduct the number(a value) of the item by one. If the number is already 1 before calling this function, remove it instead.

        void Deck::RMD(int row)
        {
            QList<QTreeWidgetItem*> s = ui->DeckList->findItems(ui->CardList->item(row, 0)->text(),
                                                                Qt::MatchFlag::MatchExactly, 1);
            if (s.isEmpty()){
                qDebug("Can't find the card on Deck::RMD");
                return;
            }
            else{
                uint8_t no = s.constFirst()->text(0).toUInt();
                if (no == 1){
                    QTreeWidgetItem *item = s.constFirst();
                    QTreeWidgetItem *parent= item->parent();
                    uint16_t row = ui->DeckList->indexFromItem(item, 1).row();
                    ui->DeckList->topLevelItem(row);
                    delete item;
                    item = NULL;
                    if (parent != NULL && parent->childCount() == 0){
                        delete parent;
                        parent = NULL;
                    }
                }
                else{
                    s.constFirst()->setText(0, QString::number(no - 1));
                }
            }
        }
        
        Taz742T Offline
        Taz742T Offline
        Taz742
        wrote on last edited by Taz742
        #11

        @MasterBlade said in How to delete an item in QTreeWidget?:

        Actually this is a very simple tree and no item has any children at all.

        If you have only topLevelItems you dont need

        QTreeWidgetItem *parent= item->parent();
        

        I created a test, in my constructor add a 3 parent like this:

            ui->treeWidget->addTopLevelItem(new QTreeWidgetItem(QStringList() << "parent"));
            ui->treeWidget->addTopLevelItem(new QTreeWidgetItem(QStringList() << "parent 1"));
            ui->treeWidget->addTopLevelItem(new QTreeWidgetItem(QStringList() << "parent 2"));
        

        okay.. I have 3 topLevelITem "parent", "parent 1" and "parent 2".

        void MainWindow::on_pushButton_clicked()
        {
            QList<QTreeWidgetItem*> s = ui->treeWidget->findItems("parent", Qt::MatchFlag::MatchExactly, 0);
        
            qDebug() << s.size(); // == 1 Because I have a only one topLevelItem which name is"parent".
        
            delete s.constFirst();
        }
        

        my topLevelItem(0) is deleted. I dont have program crash.

        Now i have a 2 topLevelItem named "parent 1" and "parent 2", now try to find a items witch contains "parent" with Qt::MatchContains flag.

        void MainWindow::on_pushButton_2_clicked()
        {
            QList<QTreeWidgetItem*> s = ui->treeWidget->findItems("parent", Qt::MatchContains, 0);
        
            qDebug() << s.size(); // == 2, Because topLevelItem "parent 1" and "parent 2" contains string "parent".
        
            foreach (QTreeWidgetItem *item, s) {
                delete item;
            }
        }
        

        Also its deleted correctly and I dont have program crash.

        Do what you want.

        M 1 Reply Last reply
        1
        • Taz742T Taz742

          @MasterBlade said in How to delete an item in QTreeWidget?:

          Actually this is a very simple tree and no item has any children at all.

          If you have only topLevelItems you dont need

          QTreeWidgetItem *parent= item->parent();
          

          I created a test, in my constructor add a 3 parent like this:

              ui->treeWidget->addTopLevelItem(new QTreeWidgetItem(QStringList() << "parent"));
              ui->treeWidget->addTopLevelItem(new QTreeWidgetItem(QStringList() << "parent 1"));
              ui->treeWidget->addTopLevelItem(new QTreeWidgetItem(QStringList() << "parent 2"));
          

          okay.. I have 3 topLevelITem "parent", "parent 1" and "parent 2".

          void MainWindow::on_pushButton_clicked()
          {
              QList<QTreeWidgetItem*> s = ui->treeWidget->findItems("parent", Qt::MatchFlag::MatchExactly, 0);
          
              qDebug() << s.size(); // == 1 Because I have a only one topLevelItem which name is"parent".
          
              delete s.constFirst();
          }
          

          my topLevelItem(0) is deleted. I dont have program crash.

          Now i have a 2 topLevelItem named "parent 1" and "parent 2", now try to find a items witch contains "parent" with Qt::MatchContains flag.

          void MainWindow::on_pushButton_2_clicked()
          {
              QList<QTreeWidgetItem*> s = ui->treeWidget->findItems("parent", Qt::MatchContains, 0);
          
              qDebug() << s.size(); // == 2, Because topLevelItem "parent 1" and "parent 2" contains string "parent".
          
              foreach (QTreeWidgetItem *item, s) {
                  delete item;
              }
          }
          

          Also its deleted correctly and I dont have program crash.

          M Offline
          M Offline
          MasterBlade
          wrote on last edited by MasterBlade
          #12

          @Taz742 said in How to delete an item in QTreeWidget?:

          Also its deleted correctly and I dont have program crash.

          Oh I'm sorry it's my fault. I'm still using item after deleting it.

          I have one more problem. I'm making children now. But they don't seem to be aligning. Is there a way to remove those spaces before them?
          0_1525689613744_1.PNG

          Taz742T 1 Reply Last reply
          0
          • M MasterBlade

            @Taz742 said in How to delete an item in QTreeWidget?:

            Also its deleted correctly and I dont have program crash.

            Oh I'm sorry it's my fault. I'm still using item after deleting it.

            I have one more problem. I'm making children now. But they don't seem to be aligning. Is there a way to remove those spaces before them?
            0_1525689613744_1.PNG

            Taz742T Offline
            Taz742T Offline
            Taz742
            wrote on last edited by Taz742
            #13

            @MasterBlade
            You dont solve this problem https://forum.qt.io/topic/90460/why-is-there-a-space-in-qtreewidget here? Why is a marked solved?

            Do what you want.

            M 1 Reply Last reply
            0
            • Taz742T Taz742

              @MasterBlade
              You dont solve this problem https://forum.qt.io/topic/90460/why-is-there-a-space-in-qtreewidget here? Why is a marked solved?

              M Offline
              M Offline
              MasterBlade
              wrote on last edited by
              #14

              @Taz742 said in How to delete an item in QTreeWidget?:

              @MasterBlade
              You dont solve this problem https://forum.qt.io/topic/90460/why-is-there-a-space-in-qtreewidget here? Why is a marked solved?

              It was solved because I didn't add any children. As you see in the pic, the first row (MDeck) is good, but its child (001_007) has that problem. Should I reopen that ticket and ask there?

              Taz742T 1 Reply Last reply
              0
              • M MasterBlade

                @Taz742 said in How to delete an item in QTreeWidget?:

                @MasterBlade
                You dont solve this problem https://forum.qt.io/topic/90460/why-is-there-a-space-in-qtreewidget here? Why is a marked solved?

                It was solved because I didn't add any children. As you see in the pic, the first row (MDeck) is good, but its child (001_007) has that problem. Should I reopen that ticket and ask there?

                Taz742T Offline
                Taz742T Offline
                Taz742
                wrote on last edited by
                #15

                @MasterBlade
                I dont understood whats wrong this picture. Can you post a image what you want exactly?

                Do what you want.

                M 1 Reply Last reply
                0
                • Taz742T Taz742

                  @MasterBlade
                  I dont understood whats wrong this picture. Can you post a image what you want exactly?

                  M Offline
                  M Offline
                  MasterBlade
                  wrote on last edited by
                  #16

                  @Taz742 said in How to delete an item in QTreeWidget?:

                  @MasterBlade
                  I dont understood whats wrong this picture. Can you post a image what you want exactly?

                  The '2' in the first column are not aligning. I want them to be in the same vertical position.

                  0_1525763326310_1.PNG
                  1_1525763326310_2.png

                  Taz742T VRoninV 2 Replies Last reply
                  0
                  • M MasterBlade

                    @Taz742 said in How to delete an item in QTreeWidget?:

                    @MasterBlade
                    I dont understood whats wrong this picture. Can you post a image what you want exactly?

                    The '2' in the first column are not aligning. I want them to be in the same vertical position.

                    0_1525763326310_1.PNG
                    1_1525763326310_2.png

                    Taz742T Offline
                    Taz742T Offline
                    Taz742
                    wrote on last edited by
                    #17

                    @MasterBlade
                    It is not possible. 2 001_007 is a child MDeck. You can add only topLevelItems.

                    Do what you want.

                    1 Reply Last reply
                    0
                    • M MasterBlade

                      @Taz742 said in How to delete an item in QTreeWidget?:

                      @MasterBlade
                      I dont understood whats wrong this picture. Can you post a image what you want exactly?

                      The '2' in the first column are not aligning. I want them to be in the same vertical position.

                      0_1525763326310_1.PNG
                      1_1525763326310_2.png

                      VRoninV Offline
                      VRoninV Offline
                      VRonin
                      wrote on last edited by
                      #18

                      @MasterBlade said in How to delete an item in QTreeWidget?:

                      I want them to be in the same vertical position.

                      Just set indentation to 0

                      "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

                      M 1 Reply Last reply
                      3
                      • VRoninV VRonin

                        @MasterBlade said in How to delete an item in QTreeWidget?:

                        I want them to be in the same vertical position.

                        Just set indentation to 0

                        M Offline
                        M Offline
                        MasterBlade
                        wrote on last edited by
                        #19

                        @VRonin said in How to delete an item in QTreeWidget?:

                        @MasterBlade said in How to delete an item in QTreeWidget?:

                        I want them to be in the same vertical position.

                        Just set indentation to 0

                        This is working!!! Many thanks!!!

                        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