PDA

View Full Version : setAlignment in Layout



vonCZ
28th April 2009, 18:03
I have a QVBoxLayout. I've set the alignment like so:


myVBLay->setAlignment(Qt::AlignRight);

If I add buttons or labels to the layout, shouldn't they Align to the Right? They don't: always left, no matter what I try. What could I possibly be missing? (I've tried setting the alignment right on the QLabel, too. Nothing.).

wysota
28th April 2009, 19:50
Yes, they should. Without seeing the code it is hard to say what you did wrong. Please provide a minimal compilable example reproducing the problem.

vonCZ
29th April 2009, 07:24
The snippet below illustrates the problem. I have a QVBoxLayout for which I've set the Alignment to AlignCenter. I've then added two labels to it. The first label is properly centered, but only because it is within it's own HBoxLayout, for which AlignCenter is set.



// Vertical Box Layout
QVBoxLayout *wlogVB_cLL = new QVBoxLayout;
wlogVB_cLL->setAlignment(Qt::AlignCenter);

// FIRST item
QHBoxLayout *wlogHB_dLL = new QHBoxLayout;
wlogHB_dLL->setAlignment(Qt::AlignCenter);
QLabel *laWellLL = new QLabel;
laWellLL->setText("First");
wlogHB_dLL->addWidget(laWellLL);
wlogVB_cLL->addLayout(wlogHB_dLL);

// SECOND item
QLabel *laWellLL2 = new QLabel;
laWellLL2->setText("Second");
wlogVB_cLL->addWidget(laWellLL2);

// Additional items, some of which are in HBoxLayouts


The second label, however: it is Aligned to the left... despite the fact that I set the *vertical* box layout to be AlignCenter. The only way I can get it to align center: put it in an HBoxLayout, just like "first".

Perhaps this is the way it's supposed to work... though I don't understand why.

My guess (I can't test it at the moment): if the VBoxLayout *only* had widgets, the setAlignment(Qt::AlignCenter) would work. But because it contains a mix of widgets and layouts, it doesn't work... and therefore *all* of the widgets must be contained in an HBoxLayout, each of which has alignment set to Center.

wysota
29th April 2009, 08:03
Everything is ok. QLayout::setAlignment(Qt::Alignment) changes the alignment of the layout, not the items inside it. You should use QLayout::setAlignment(QWidget *, Qt::Alignment) variant instead.