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

How to delete a row of a QGridLayout

Scheduled Pinned Locked Moved Unsolved General and Desktop
20 Posts 5 Posters 9.9k 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.
  • 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