[Qt-interest] QHBoxLayout alignment issue
Bo Thorsen
bo at fioniasoftware.dk
Fri Mar 4 12:07:20 CET 2011
Den 04-03-2011 10:58, Markus Franke skrev:
>
> Dear all,
>
> I have three widgets (QLabel's) which I would like to place into a
> QHBoxLayout.
>
> ---------------------------------------------------------------------------------------
>
> | Widget 1 | Widget 2 | Widget 3 |
> ---------------------------------------------------------------------------------------
>
>
> Widget 1 should always be aligned left, Widget 3 should always be
> aligned right.
> Widget 2 should always be centered in the middle of Widget 1 and Widget
> 3, i.e. the gaps Widget1-Widget2 and Widget2-Widget3 should always be of
> equal size.
>
> I did something like below but Widget 2 still is not equally aligned
> between Widget 1 and Widget 3.
>
> ---snip---
> Widget1->setAlignment(Qt::AlignLeft);
> Widget2->setAlignment(Qt::AlignHCenter);
> Widget3->setAlignment(Qt::AlignRight);
>
> QHBoxLayout* HValues = new QHBoxLayout;
>
> HValues->addWidget(Widget1, 0, Qt::AlignLeft);
> HValues->addWidget(Widget2, 1, Qt::AlignCenter);
> HValues->addWidget(Widget3, 2, Qt::AlignRight);
> HValues->setSpacing(5);
> ---snip---
>
> How can I achieve this behaviour with QHBoxLayout?
You have to set the stretch on the middle entry to 1 and the others to
0. This way widget 1 and 3 won't get any space, and widget 2 gets all of it.
Use:
HValues->addWidget(Widget1, 0, Qt::AlignLeft);
HValues->addWidget(Widget2, 1, Qt::AlignCenter);
HValues->addWidget(Widget3, 0, Qt::AlignRight); // << this was changed
HValues->setSpacing(5);
Bo Thorsen,
Fionia Software.
--
Expert Qt and C++ developer for hire
Contact me if you need expert Qt help
http://www.fioniasoftware.dk
More information about the Qt-interest-old
mailing list