Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

How to get row and columns count in QGridLayout?



  • 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 )?



  • @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!



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



  • @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! :)


Log in to reply