The Bearded Templar
The Bearded Templar

Reputation: 701

Dynamic row length in a QGridLayout

I'm looking to make an image viewer using PySide and Qt, where all the images are of fixed size and arranged in a grid. I know how to set up the basic code for this by defining a QGridLayout such as this:

l = QGridLayout(10,10)
l.addItem(im1,0,0)
l.addItem(im2,0,1)
...

However, I also want the number of elements per row to resize dynamically. I will know in advance how many images there are, but I won't know how big the dialog will be. So if the user makes the dialog very small, there should be 2 images per line, but if they maximize it, there should be (e.g.) 10 images per line. Is there a way to make a QGridLayout that adjusts the row size dynamically?

Upvotes: 0

Views: 151

Answers (1)

qurban
qurban

Reputation: 3945

You don't need to use QGridLayout for this purpose, use FlowLayout instead. FlowLayout is not a built-in layout in Qt but it is developed by someone else, you can use it by just copying FlowLayout class to your project file. this is the example of FlowLayout. Copy the whole code to your IDE and run it (remember to change the import statements at the top to PySide as that code is using PyQt4). After executing the code, resize the window that appears, you will see that the pushButtons inside the window will be adjusted automatically.

Upvotes: 1

Related Questions