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 get row and columns count in QGridLayout?
Forum Update on Monday, May 27th 2025

How to get row and columns count in QGridLayout?

Scheduled Pinned Locked Moved Solved General and Desktop
4 Posts 2 Posters 3.3k 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.
  • T Offline
    T Offline
    TomNow99
    wrote on 30 Nov 2020, 12:38 last edited by
    #1

    Hi,

    I have QGridLayout with 10 widgets on it ( 2 rows, 5 columns ). Next I delete all widgets from this layout using code:

                  QLayoutItem *item;
    
                  while((item = lay->takeAt(0)) != 0)
                  {
                      if (item->widget())
                      {
                          lay->removeWidget(item->widget());
                          delete item->widget();
                      }
    
                      delete item;
                  }
    

    Now I would like to check how many row and columns has this layout, so I use rowCount(), columnCount(). I see numbers 2 and 5. I excepted 0 and 0.

    So how can I get the real row number and column number ( 0, 0 )?

    J 1 Reply Last reply 30 Nov 2020, 13:21
    0
    • T TomNow99
      30 Nov 2020, 12:38

      Hi,

      I have QGridLayout with 10 widgets on it ( 2 rows, 5 columns ). Next I delete all widgets from this layout using code:

                    QLayoutItem *item;
      
                    while((item = lay->takeAt(0)) != 0)
                    {
                        if (item->widget())
                        {
                            lay->removeWidget(item->widget());
                            delete item->widget();
                        }
      
                        delete item;
                    }
      

      Now I would like to check how many row and columns has this layout, so I use rowCount(), columnCount(). I see numbers 2 and 5. I excepted 0 and 0.

      So how can I get the real row number and column number ( 0, 0 )?

      J Offline
      J Offline
      JonB
      wrote on 30 Nov 2020, 13:21 last edited by
      #2

      @TomNow99
      ? A QGridLayout has rows/columns as per whatever items you add via addItem/Layout/Widget(). Since you remove all of these, it now has 0 rows/columns!

      1 Reply Last reply
      0
      • T Offline
        T Offline
        TomNow99
        wrote on 30 Nov 2020, 13:25 last edited by
        #3

        @JonB Thank you for answer

        After delete widgets from QGridLayout ( it's empty ) I try add a few widgets for example 4. I still see that layout have set rows = 2 , columns = 5. When I check this using layout->columnCount() I see 5. Look ( RA. answer ):

        https://stackoverflow.com/questions/13405997/delete-a-row-from-qgridlayout

        J 1 Reply Last reply 30 Nov 2020, 13:30
        0
        • T TomNow99
          30 Nov 2020, 13:25

          @JonB Thank you for answer

          After delete widgets from QGridLayout ( it's empty ) I try add a few widgets for example 4. I still see that layout have set rows = 2 , columns = 5. When I check this using layout->columnCount() I see 5. Look ( RA. answer ):

          https://stackoverflow.com/questions/13405997/delete-a-row-from-qgridlayout

          J Offline
          J Offline
          JonB
          wrote on 30 Nov 2020, 13:30 last edited by JonB
          #4

          @TomNow99
          Yes, I had realized this after I typed my answer, and was thinking of deleting it!

          So since as you say the accepted answer there is at https://stackoverflow.com/a/13406780/489865, isn't that as good as it gets?

          But otherwise it refers you to https://stackoverflow.com/a/19256990/489865, where that solution states:

          Removing a row or column (or even a single cell) from a QGridLayout is tricky

          First, note that QGridLayout::rowCount() and QGridLayout::columnCount() always return the number of internally allocated rows and columns in the grid layout.

          Note that it's unfortunately impossible to remove such an internal row or column from the grid layout. In other words, the row and column count of a grid layout can always only grow, but never shrink.

          So isn't that your answer? You seem to be saying you don't like the answers there! :)

          1 Reply Last reply
          1

          1/4

          30 Nov 2020, 12:38

          • Login

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