10

The question sais it all.

I have a 5 x 3 grid.

It looks like this

row 0:  buttonA--buttonA--buttonA   nothing   buttonB--buttonB--buttonB

row 1:  empty row

row 2:  buttonC  nothing  buttonD   nothing   buttonE  nothing  buttonF     

In the spaces where there is nothing in the whole column or row the minimumRowHeight and all of those settings would work. Right?

Well. The button A and button B (and the rest), never get centered over those 3 cells they share. NEVER. doesn´t matter what i write on the parameters for the layout.

I erased the code because it finally got it using a Vertical Layout, and inside, two horizontal layouts. Those get centered that way. But i would like to position them better.

What doesn´t work is when adding a widget, using:

addWidget( widget, 0,0 , 2,0 , Qt::AlignHCenter ); not even AlignCenter works.

How to make it centered or aligned to the right?

Thanks!

Darkgaze
  • 2,280
  • 6
  • 36
  • 59
  • I found out some posts that tell to add a dummy widget expandable inside the 3 cell space. Then insert a layout on it and the widget we wanted to be centered. Would it work? isn´t it so complicated? – Darkgaze Jan 11 '13 at 11:40
  • As a side note, you showed a 7 x 3 grid, not a 5 x 3 one – Tim Meyer Jan 11 '13 at 12:04
  • If you want to make buttonA being stretched over 3 columns, you have to do "addWidget( widget, 0, 0, **0, 3** );" Try if this works – Tim Meyer Jan 11 '13 at 12:06
  • what?..... but it goes from column 0 to column 3 -> 4 colums! – Darkgaze Jan 14 '13 at 11:24
  • your buttonA is in columns 0, 1, 2, then in column 3 is "nothing", then buttonB is in 4, 5, 6 (at least in the example you provided). Anyway I just wanted to point out you have to set the last number, your code stretches it over 2 rows, not 3 columns – Tim Meyer Jan 14 '13 at 11:46
  • Oh. I understood it was, the position of start (topleft corner) and the ending (bottom right corner). That´s why it goes from 0,0 to 2,0 ... but if it´s the stretch , and the numbers start on 1, then it´s 3. i´ll give it a try and i´ll check your answer (you could just write the answer down there so i can check it and you want the reputation, or i write it myself. :-) ) – Darkgaze Jan 15 '13 at 10:01
  • I wrote a detailed answer which should clear up any misunderstanding – Tim Meyer Jan 15 '13 at 11:59

1 Answers1

18

The syntax of QGridLayout::addWidget() is like this:

void QGridLayout::addWidget ( QWidget * widget, int fromRow, int fromColumn, int rowSpan, int columnSpan, Qt::Alignment alignment = 0 )

with the description:

This version adds the given widget to the cell grid, spanning multiple rows/columns. The cell will start at fromRow, fromColumn spanning rowSpan rows and columnSpan columns.

This means your line

addWidget( widget, 0, 0, 2, 0, Qt::AlignHCenter ); 

has fromRow = 0, fromColumn = 0, rowSpan = 2 and columnSpan = 0. This means it starts from row 0 and spans over two rows, i.e. it will be in row 0 and 1 (Note: Two rows in total, not two additional rows). Also it starts from column 0 with a span of 0 which I think means the column span is ignored.

So what you really want should be:

addWidget( widget, 0, 0, 0, 3, Qt::AlignCenter );

You might have to experiment with the alignment a bit.

Community
  • 1
  • 1
Tim Meyer
  • 12,210
  • 8
  • 64
  • 97