PDA

View Full Version : QGridLayout vertical spacing



tvj4218
12th January 2018, 20:45
Hi all.

I create a QGridLayout programatically and then add various widgets to it in the zeroth column of the first few rows.
This looks fine except that there is a lot of space between widgets/rows when I run the app.
I've added a vertical spacer in the last row, which does push things up. (Desired behavior.)
I've tried using setContentMargins and setSpacing with no luck.

Any ideas?

Thanks in advance.

n

d_stranz
12th January 2018, 21:22
I've added a vertical spacer in the last row, which does push things up. (Desired behavior.)

OK then, if that's what you want, what's wrong with that? If you were using Qt Designer to lay out the grid statically, you would do exactly the same.

Setting margins and spacing only establish minimums. When you make the container larger, the grid will add more space between rows and columns to fit the larger space. The only way to to constrain the grid to the preferred sizes of its widgets plus spacing is to use a spacer.

By the way, setContentsMargins() has no effect on the spacing around the widgets in the grid (except with respect to laying out the whole grid). It establishes the margin around the entire grid layout within its parent.