View Full Version : Limit number of row/column in QGridLayout

18th October 2007, 20:26
First of all, hello to everyone, I'm a newbie hehe.

Just began integration of Qt in our application and I know this is a stupid question but I can't find the solution and I tried. I even got in the source code which was really interesting.

I want to use QGridLayout but limit the number of column. I know it was possible in Qt3 but in Qt4, the constructors have disappeared. It would also be nice to do it after contruction if possible as this may change dynamically.

Also how do I set the default orientation for adding new widget. I looked in the source code and there is setDefaultPositioning but it doesn't seem to do what I want.

For example, I would like to be able to create a QGridLayout. Set max column number to 3 and add every new item in the next column and switch row when necessary using the default addWidget( Widget* ).

18th October 2007, 20:37
I don't think what you want to do is possible in Qt 4. The only addWidget( ) take in the row/column combination to tell it where to place it as well as the spanning row and column function.

The only way I can see how you could do this the way you want is to create a variable holding columnNum and rowNum. Then have a number stating MAX_COLS, like a #define or something. After adding a widget, increment columnNum and then when columnNum >= MAX_COLS set columnNum back to 0 and increment rowNum. This is the only way I can think of how you can solve your problem, but I have always just said what row and column to add things to.

I also don't think this was possible in Qt 3.3 as I have worked a lot in Qt 3.3 and looked in its documentation and the addWidget function requires a row and column be passed in. If you want to just add items without telling space, maybe you should look into using a QHBoxLayout containing "x" number of QVBoxLayouts. But again you would have to store some value stating what column you were on and what the max number of columns are.

If you don't put any items into a column, that column will not exist. So if you only add items to columns 0-2 then only columns 0-2 will show. Then later on if you add something to column 3, then 0-3 will all be visible. If no items are in a column or column after it, the column will not exist.

18th October 2007, 20:54
QLayout has addWidget( Widget* ) which calls virtual addItem( QLayoutItem* ) which is defined in QGridLayout. I saw some code about positioning and updating next insert pos, I just can't find how to set the maximum number of rows and columns.