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 a row of a QGridLayout
Forum Updated to NodeBB v4.3 + New Features

How to delete a row of a QGridLayout

Scheduled Pinned Locked Moved Unsolved General and Desktop
20 Posts 5 Posters 9.5k Views 2 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 Offline
    M Offline
    Maxime0601
    wrote on last edited by
    #1

    Hello,
    I have to make a button able to delete the last row of a QGridLayout. Sure, I used signals and slots but I don't arrive to create a functional slot.
    In addition, I want that the user is able to add row at this Grid (I succeed for that) but he can't add an unlimited number (I use an if condition and count the number of row of my grid).
    So I tried to use the hide method but after many delete the user can't add rows. I also tried with takeAt(), removeWidget(), removeItem().

    Thanks a lot if you have an idea to do that. (I'm in begginer in QT)

    1 Reply Last reply
    0
    • Christian EhrlicherC Online
      Christian EhrlicherC Online
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Please show us what you did.

      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
      0
      • Pl45m4P Offline
        Pl45m4P Offline
        Pl45m4
        wrote on last edited by
        #3

        @maxime0601

        What do you mean by "deleting a row"? You have to remove the Widgets / Items from your layout at that position. A layout is defined by its widgets / items.

        You could delete / remove all items in a specific row, but there is no layout-"row" that you can delete.


        If debugging is the process of removing software bugs, then programming must be the process of putting them in.

        ~E. W. Dijkstra

        1 Reply Last reply
        1
        • M Offline
          M Offline
          Maxime0601
          wrote on last edited by
          #4
          void MyGridBox::removeRow(void){
              int row = this->rowCount();
              if (row > 6)
              {
                  QLayoutItem *child; //Taken from takeAt(int) documentation
                  while ((child = this->takeAt(row)) != nullptr) {
                      child->widget()->hide();
                      delete child;
                  }
          //        for (int i(0); i > 4 ; ++i){
          //            this->itemAtPosition(row,i)->widget()->hide();
          //            delete this->itemAtPosition(row,i);
          //        }
              }
          }
          

          @Pl45m4 I want to remove the widgets of the row but the row doesn't disappear and I can't explain why. If I add some rows after deleting one, the limit of the number of rows change.

          @Christian-Ehrlicher the part which are in commentary is an another try. And the other part is inspired of the doc and it was the better result of my tries.

          JonBJ 1 Reply Last reply
          0
          • M Maxime0601
            void MyGridBox::removeRow(void){
                int row = this->rowCount();
                if (row > 6)
                {
                    QLayoutItem *child; //Taken from takeAt(int) documentation
                    while ((child = this->takeAt(row)) != nullptr) {
                        child->widget()->hide();
                        delete child;
                    }
            //        for (int i(0); i > 4 ; ++i){
            //            this->itemAtPosition(row,i)->widget()->hide();
            //            delete this->itemAtPosition(row,i);
            //        }
                }
            }
            

            @Pl45m4 I want to remove the widgets of the row but the row doesn't disappear and I can't explain why. If I add some rows after deleting one, the limit of the number of rows change.

            @Christian-Ehrlicher the part which are in commentary is an another try. And the other part is inspired of the doc and it was the better result of my tries.

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

            @maxime0601

            int row = this->rowCount();
            ...
            while ((child = this->takeAt(row)) != nullptr) {
            

            Don't rows (for takeAt(row)) count from 0 to rowCount() - 1? So your code will do nothing (docs: "If there is no such item, the function must do nothing and return 0."), and never enter the while loop?

            As for the commented-out attempt:

            //        for (int i(0); i > 4 ; ++i){
            //            this->itemAtPosition(row,i)->widget()->hide();
            //            delete this->itemAtPosition(row,i);
            //        }
            

            won't do what you intend because you delete at i and then increment i, not taking into account that you have done a deletion which squeezes other items down (i.e. it deletes every other item)? That apart, i starts at 0 and your loop while condition is i > 4, so it will never enter the loop anyway.

            In both these cases you would find out what is/is not happening if you put some kind of trace/debug statements into your code.

            M 1 Reply Last reply
            1
            • JonBJ JonB

              @maxime0601

              int row = this->rowCount();
              ...
              while ((child = this->takeAt(row)) != nullptr) {
              

              Don't rows (for takeAt(row)) count from 0 to rowCount() - 1? So your code will do nothing (docs: "If there is no such item, the function must do nothing and return 0."), and never enter the while loop?

              As for the commented-out attempt:

              //        for (int i(0); i > 4 ; ++i){
              //            this->itemAtPosition(row,i)->widget()->hide();
              //            delete this->itemAtPosition(row,i);
              //        }
              

              won't do what you intend because you delete at i and then increment i, not taking into account that you have done a deletion which squeezes other items down (i.e. it deletes every other item)? That apart, i starts at 0 and your loop while condition is i > 4, so it will never enter the loop anyway.

              In both these cases you would find out what is/is not happening if you put some kind of trace/debug statements into your code.

              M Offline
              M Offline
              Maxime0601
              wrote on last edited by
              #6

              @jonb
              For the part with the takeAt(row), I think that I haven't really understand the method. However, it works but it delete more than I hoped.

              For the other part, the i > 4 is just a mistake when I edited my code but with i < 4 the program crashs. If I understand well your explain I just have to delete the first element and after it will delete others under it?

              Pl45m4P 1 Reply Last reply
              0
              • M Maxime0601

                @jonb
                For the part with the takeAt(row), I think that I haven't really understand the method. However, it works but it delete more than I hoped.

                For the other part, the i > 4 is just a mistake when I edited my code but with i < 4 the program crashs. If I understand well your explain I just have to delete the first element and after it will delete others under it?

                Pl45m4P Offline
                Pl45m4P Offline
                Pl45m4
                wrote on last edited by
                #7

                @maxime0601 said in How to delete a row of a QGridLayout:

                I edited my code but with i < 4 the program crashs

                i < 4 only works if you have 4 items in your Grid, otherwise there is no column 3.

                You have to replace the 4 with the number of your current cols.


                If debugging is the process of removing software bugs, then programming must be the process of putting them in.

                ~E. W. Dijkstra

                M 1 Reply Last reply
                0
                • Pl45m4P Pl45m4

                  @maxime0601 said in How to delete a row of a QGridLayout:

                  I edited my code but with i < 4 the program crashs

                  i < 4 only works if you have 4 items in your Grid, otherwise there is no column 3.

                  You have to replace the 4 with the number of your current cols.

                  M Offline
                  M Offline
                  Maxime0601
                  wrote on last edited by
                  #8

                  @pl45m4

                      int row = this->rowCount();
                      int col = this->columnCount();
                      if (row > 6)
                      {
                          for (int i(0); i < col ; ++i){
                              this->itemAtPosition(row,i)->widget()->hide();
                              delete this->itemAtPosition(row,i);
                          }
                      }
                  }
                  

                  It also crashs like this.

                  JonBJ Pl45m4P 2 Replies Last reply
                  0
                  • M Maxime0601

                    @pl45m4

                        int row = this->rowCount();
                        int col = this->columnCount();
                        if (row > 6)
                        {
                            for (int i(0); i < col ; ++i){
                                this->itemAtPosition(row,i)->widget()->hide();
                                delete this->itemAtPosition(row,i);
                            }
                        }
                    }
                    

                    It also crashs like this.

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

                    @maxime0601
                    I already said/suggested

                    int row = this->rowCount();
                    ...
                    this->itemAtPosition(row,i)->widget()->hide();
                    

                    Don't rows count from 0 to this->rowCount() - 1? So your row exceeds the number of rows, hence crashes? Test what this->itemAtPosition(row, 0) returns, or crashes?

                    Then when you have this right, I don't think you should directly delete an item (which is still owned by the grid layout), shouldn't you do some kind of:

                    delete this->takeAt(?)
                    
                    M 1 Reply Last reply
                    2
                    • M Maxime0601

                      @pl45m4

                          int row = this->rowCount();
                          int col = this->columnCount();
                          if (row > 6)
                          {
                              for (int i(0); i < col ; ++i){
                                  this->itemAtPosition(row,i)->widget()->hide();
                                  delete this->itemAtPosition(row,i);
                              }
                          }
                      }
                      

                      It also crashs like this.

                      Pl45m4P Offline
                      Pl45m4P Offline
                      Pl45m4
                      wrote on last edited by
                      #10

                      @maxime0601

                      Try to remove the widget from your layout (instead of hiding it).


                      If debugging is the process of removing software bugs, then programming must be the process of putting them in.

                      ~E. W. Dijkstra

                      1 Reply Last reply
                      0
                      • JonBJ JonB

                        @maxime0601
                        I already said/suggested

                        int row = this->rowCount();
                        ...
                        this->itemAtPosition(row,i)->widget()->hide();
                        

                        Don't rows count from 0 to this->rowCount() - 1? So your row exceeds the number of rows, hence crashes? Test what this->itemAtPosition(row, 0) returns, or crashes?

                        Then when you have this right, I don't think you should directly delete an item (which is still owned by the grid layout), shouldn't you do some kind of:

                        delete this->takeAt(?)
                        
                        M Offline
                        M Offline
                        Maxime0601
                        wrote on last edited by
                        #11

                        @jonb It continues to crash

                        @Pl45m4 I also tried with

                        this->removeWidget(itemAtPosition(row,0)->widget()->hide();
                        

                        and it crashes too.

                        jsulmJ JonBJ 2 Replies Last reply
                        0
                        • M Maxime0601

                          @jonb It continues to crash

                          @Pl45m4 I also tried with

                          this->removeWidget(itemAtPosition(row,0)->widget()->hide();
                          

                          and it crashes too.

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

                          @maxime0601 said in How to delete a row of a QGridLayout:

                          It continues to crash

                          then please use the debugger to see what exactly happens and where...

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

                          M 1 Reply Last reply
                          0
                          • M Maxime0601

                            @jonb It continues to crash

                            @Pl45m4 I also tried with

                            this->removeWidget(itemAtPosition(row,0)->widget()->hide();
                            

                            and it crashes too.

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

                            @maxime0601

                            @jonb It continues to crash

                            @Pl45m4 I also tried with

                            this->removeWidget(itemAtPosition(row,0)->widget()->hide();

                            and it crashes too.

                            Last time of saying: what does itemAtPosition(row,0) return/do, given your value of row?? How difficult is it to test that?

                            1 Reply Last reply
                            0
                            • jsulmJ jsulm

                              @maxime0601 said in How to delete a row of a QGridLayout:

                              It continues to crash

                              then please use the debugger to see what exactly happens and where...

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

                              @jsulm I have a signal of segmentation fault. It confirms that I try to use an outside element of my grid

                              jsulmJ 1 Reply Last reply
                              0
                              • M Maxime0601

                                @jsulm I have a signal of segmentation fault. It confirms that I try to use an outside element of my grid

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

                                @maxime0601 said in How to delete a row of a QGridLayout:

                                I try to use an outside element of my grid

                                Well, then fix that. And I know that it is crashing and my suggestion to use debugger is still valid: you will see where exactly it is crashing and what the values of variables at that time.
                                Did you read what @JonB wrote?

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

                                1 Reply Last reply
                                0
                                • M Offline
                                  M Offline
                                  Maxime0601
                                  wrote on last edited by Maxime0601
                                  #16

                                  @jonb I'm sorry if I don't understand what you mean but when I tried itemAtPosition(row,0) it crashes.
                                  @jsulm the problem is at column 4 and row 7 and it is the position of one of the element that I want to delete.

                                  Oh I tried with row - 1 and it seems works

                                  JonBJ 1 Reply Last reply
                                  0
                                  • M Maxime0601

                                    @jonb I'm sorry if I don't understand what you mean but when I tried itemAtPosition(row,0) it crashes.
                                    @jsulm the problem is at column 4 and row 7 and it is the position of one of the element that I want to delete.

                                    Oh I tried with row - 1 and it seems works

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

                                    @maxime0601

                                    @jonb I'm sorry if I don't understand what you mean but when I tried itemAtPosition(row,0) it crashes.

                                    Which is why I said your program crashes all the time! You are not understanding that rows are numbered starting from 0 and going to rowCount() - 1, not to rowCount(). That's how most arrays etc. are numbered in C/C++. I don't know how to explain any better, you really need to understand this fundamental, rather than banging stuff in without understanding:

                                    Oh I tried with row - 1 and it seems works

                                    You really need to understand why that it is, rather than "seems to work".

                                    1 Reply Last reply
                                    2
                                    • M Offline
                                      M Offline
                                      Maxime0601
                                      wrote on last edited by
                                      #18
                                      int row = this->rowCount();
                                      int col = this->columnCount();
                                      if (row > 6)
                                      {
                                         for (int i(0); i < col ; ++i)
                                         {
                                               this->itemAtPosition(row - 1,i)->widget()->hide();
                                               delete this->takeAt(row - 1);
                                         }
                                      }
                                      

                                      So, when I used this code, the last row disappeared as I want but the disposition of the others elements in the grid changes.

                                      @JonB yes I knew this for vector but I totally forgot it. I'm sorry for that.

                                      JonBJ 1 Reply Last reply
                                      0
                                      • M Maxime0601
                                        int row = this->rowCount();
                                        int col = this->columnCount();
                                        if (row > 6)
                                        {
                                           for (int i(0); i < col ; ++i)
                                           {
                                                 this->itemAtPosition(row - 1,i)->widget()->hide();
                                                 delete this->takeAt(row - 1);
                                           }
                                        }
                                        

                                        So, when I used this code, the last row disappeared as I want but the disposition of the others elements in the grid changes.

                                        @JonB yes I knew this for vector but I totally forgot it. I'm sorry for that.

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

                                        @maxime0601
                                        I'm sorry but it's more complex than you are expecting. You really need to read https://stackoverflow.com/questions/5395266/removing-widgets-from-qgridlayout. I know it looks a lot more involved than what you are trying, but there you are....

                                        M 1 Reply Last reply
                                        1
                                        • JonBJ JonB

                                          @maxime0601
                                          I'm sorry but it's more complex than you are expecting. You really need to read https://stackoverflow.com/questions/5395266/removing-widgets-from-qgridlayout. I know it looks a lot more involved than what you are trying, but there you are....

                                          M Offline
                                          M Offline
                                          Maxime0601
                                          wrote on last edited by
                                          #20

                                          @jonb Thanks a lot, I've searched but I don't found similar topic. I think that I can succeed with this.
                                          Thanks a lot everyone for your help

                                          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