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?

How to delete an item in QTreeWidget?

Scheduled Pinned Locked Moved Solved General and Desktop
19 Posts 5 Posters 13.9k 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.
  • M MasterBlade
    7 May 2018, 00:33

    Hello, I searched the tree and found an item. I want to delete it.

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

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

    Then I tried

            QTreeWidgetItem *item = s.constFirst();
            uint16_t row = ui->DeckList->indexFromItem(item, 1).row();
            ui->DeckList->topLevelItem(row);
    

    But it won't pass.

    C:\Qt\5.10.1\mingw53_32\include\QtWidgets\qtreewidget.h:368: error: 'QModelIndex QTreeWidget::indexFromItem(QTreeWidgetItem*, int) const' is protected
    QModelIndex indexFromItem(QTreeWidgetItem *item, int column = 0) const; // ### Qt 6: remove

    What should I do?

    T Offline
    T Offline
    Taz742
    wrote on 7 May 2018, 06:19 last edited by Taz742 5 Jul 2018, 07:06
    #7

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

    Do what you want.

    M 1 Reply Last reply 7 May 2018, 07:45
    1
    • C Christian Ehrlicher
      7 May 2018, 04:28

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

      M Offline
      M Offline
      MasterBlade
      wrote on 7 May 2018, 06:48 last edited by
      #8

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

      T 1 Reply Last reply 7 May 2018, 06:58
      0
      • M MasterBlade
        7 May 2018, 06:48

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

        T Offline
        T Offline
        Taz742
        wrote on 7 May 2018, 06:58 last edited by
        #9

        @MasterBlade
        What if it does not have a parent?

        Do what you want.

        1 Reply Last reply
        0
        • T Taz742
          7 May 2018, 06:19

          @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 7 May 2018, 07:45 last edited by MasterBlade 5 Jul 2018, 07:51
          #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));
                  }
              }
          }
          
          T 1 Reply Last reply 7 May 2018, 08:51
          0
          • M MasterBlade
            7 May 2018, 07:45

            @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));
                    }
                }
            }
            
            T Offline
            T Offline
            Taz742
            wrote on 7 May 2018, 08:51 last edited by Taz742 5 Jul 2018, 09:11
            #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 7 May 2018, 10:40
            1
            • T Taz742
              7 May 2018, 08:51

              @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 7 May 2018, 10:40 last edited by MasterBlade 5 Jul 2018, 10:40
              #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

              T 1 Reply Last reply 7 May 2018, 10:47
              0
              • M MasterBlade
                7 May 2018, 10:40

                @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

                T Offline
                T Offline
                Taz742
                wrote on 7 May 2018, 10:47 last edited by Taz742 5 Jul 2018, 10:48
                #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 8 May 2018, 00:35
                0
                • T Taz742
                  7 May 2018, 10:47

                  @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 8 May 2018, 00:35 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?

                  T 1 Reply Last reply 8 May 2018, 05:27
                  0
                  • M MasterBlade
                    8 May 2018, 00:35

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

                    T Offline
                    T Offline
                    Taz742
                    wrote on 8 May 2018, 05:27 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 8 May 2018, 07:10
                    0
                    • T Taz742
                      8 May 2018, 05:27

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

                      M Offline
                      M Offline
                      MasterBlade
                      wrote on 8 May 2018, 07:10 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

                      T V 2 Replies Last reply 8 May 2018, 07:13
                      0
                      • M MasterBlade
                        8 May 2018, 07:10

                        @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

                        T Offline
                        T Offline
                        Taz742
                        wrote on 8 May 2018, 07:13 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
                          8 May 2018, 07:10

                          @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

                          V Offline
                          V Offline
                          VRonin
                          wrote on 8 May 2018, 07:24 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 8 May 2018, 08:17
                          3
                          • V VRonin
                            8 May 2018, 07:24

                            @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 8 May 2018, 08:17 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

                            16/19

                            8 May 2018, 07:10

                            • Login

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