PDA

View Full Version : How to set fixed row and column number in QGridLayout



Seishin
19th June 2015, 02:00
I'm trying to add widgets in QGridLayout with fixed row number and column number, so a widget will always on the same location regardless of the other rows or columns are empty or not.
For example, I want to make a layout with 2 rows and 3 columns.
When I have 5 widgets, it always looks like
11221
However, if I only have 2 widgets, I want it looks like
11222
But NOT looks like
11224
which is default by QGridLayout because I cannot specify the row and column number until I add a widget on that row/column.

Right now I found few ways to do it.
One Way is always add a dummy widget at position x_max, y_max, which are number of row and column, and remove it when a real widget need to be placed.

Another way is change the alignment not to be center, but then I have a lot margin settings need to be manually calculated because I want each widget still centered in its position.

Do you have any better way to do it? Thanks in advance.

Seishin
20th June 2015, 19:11
Anyone has ideas? I think this should be a general requirements (like the layout in android and ios). I will be surprised if Qt doesn't have a intuitive way to do it.

d_stranz
20th June 2015, 20:30
Layouts are designed to be dynamic and change their configuration as their content changes. So the grid layout is doing what it is supposed to do as you add widgets to it. If you want to force a given row x cell count, then you should probably add QSpacerItem instances to at least one of each of the left / right and top / bottom edge cells as placeholders to force the rest of the cells to the upper left.

anda_skoa
21st June 2015, 11:23
If you want to force a given row x cell count, then you should probably add QSpacerItem instances to at least one of each of the left / right and top / bottom edge cells as placeholders to force the rest of the cells to the upper left.

Or even have an additional fourth column that contains vertical spacers which ensure each row has the desired height and a third row with horizontal spacers to ensure each column has the desired width.

Cheers,
_

Seishin
22nd June 2015, 16:58
Yeah I think adding an item will be the way now. Surprised Qt doesn't have a better way to do it.

d_stranz
22nd June 2015, 18:54
Surprised Qt doesn't have a better way to do it.

Like I said, Qt layouts are designed to adapt dynamically to their content. You're asking grid layout to do something unnatural - adopt a fixed layout regardless of content. You can either develop your own "fixed grid layout" or you can (through code or Qt Designer) add spacers to force the behavior you want. That's about as good as it gets.

Seishin
26th June 2015, 19:42
Like I said, Qt layouts are designed to adapt dynamically to their content. You're asking grid layout to do something unnatural - adopt a fixed layout regardless of content. You can either develop your own "fixed grid layout" or you can (through code or Qt Designer) add spacers to force the behavior you want. That's about as good as it gets.

I don't think the fixed row/column layout is unnatural. On my Android phone I set the home screen to be 4 rows and 4 columns on each page. Regardless of how my apps I placed on one page, they must located on one of the 16 positions. I think this should be common for most of the desktop/home screen, isn't it?

d_stranz
27th June 2015, 00:50
It is unnatural for a QLayout. Of course there are other GUIs in use where things are placed on a fixed grid, like your phone, Excel spreadsheets, or a calendar. Geez, don't take things so literally.