Search code examples
qtuser-interfacelayoutqtwidgets

How to update QGridLayout row/column count depending on the layout size?


This thing seems really basic to me, though I could not find any information on how to implement it.

I have some sort of QGridLayout that contains equally-sized widgets. I would like to update its row/column count based on the layout size.

For instance, on a relatively wide screen, it would look like this:

wide screen

But if the screen is not wide enough to fit all the items it would grow vertically like this:

narrow screen


Solution

  • The QGridLayout doesn't support this automatically. You can either:

    1. Use QGridLayout with a number of columns set according to screen width divided by the widget width, and the number of rows sufficient to fit the widgets, or

    2. Use the flow layout example and modify to your needs.