QBoxLayout and "stretching"
-
Trying to implement something similar to a toolbar where the elements are fixed in size and align to the left.
When i implement QBoxLayout and add a few widgets there, they seem to "spread" evenly across the expanse of the layout object. I'd like them to align to the left and have no spacing between them at all, which also means, large empty area to the right of the last widget as the sum width of all widgets is smaller than the QBoxLayout object.
For some reason, none of the following produces the desired effect. What am I forgetting?
@this->buttonLayout->setDirection(QBoxLayout::LeftToRight);
this->buttonLayout->setAlignment(Qt::AlignLeft);
this->buttonLayout->setSpacing(0);
this->buttonLayout->setContentsMargins(0, 0, 0, 0);@ -
Insert a "QSpacerItem":http://doc.trolltech.com/4.7/qspaceritem.html into your layout.
-
I added:
@this->buttonLayout->addSpacerItem(new QSpacerItem(0,0, QSizePolicy::Expanding,QSizePolicy::Expanding ));@
but there is no change in the dynamically growing spaces between the widgets I add. It keeps the relative size of the width as I grow and shrink the window. Are you sure this should be enough?
-
It makes no difference if I put the "addWidget" x 5 times, before or after the addSpacerItem call. It's always the same. The widgets just spread out evenly. I must be doing something wrong. This is very strange. I'll try doing this graphically in the 'Designer' and take a look at the generated code.
-
For example:
@
QHBoxLayout *layout = new QHBoxLayout;
layout->setSpacing(0);
layout->setContentsMargins(0, 0, 0, 0);
layout->addWidget(new QPushButton("Button1"));
layout->addWidget(new QPushButton("Button2"));
layout->addWidget(new QPushButton("Button3"));
layout->addSpacerItem(new QSpacerItem(0,0,QSizePolicy::Expanding, QSizePolicy::Expanding));
@ -
I was able to do it like that, however I have a stranger problem. Spacing is twisted.
my widgets are of variable widths (same height). I implemented the sizeHint method in the class of my widgets. If I set spacing to be "0", there is an overlap and ALL widgets are spaced such that they look even in width. very strange. If I do "-1" in the spacing, they will be okay with the variable widths, but will have a ~2px spacing between them.
I've checked and the sizeHint returns the correct size any time...
I can't figure this weirdness out. Trying more things...
-
Why does spacing "0" cause an overlap of my widgets? it takes the width of the last widget and applies it to all the prior siblings which are wider. Very strange behavior. A spacing of "-1" spaces the variable width widgets okay, but with a 2 pixel spacing between widgets. I just don't get it.
My widgets set "setMinimumSize" which is correct, and also implement hintSize which I checked gives out a correct value. Why is the layout messing things up with spacing "0"? is this a Qt bug?
-
A more "scientific" observation.
I have 3 widgets in the layout.when spacing is set to 0, the first two (all are of variable width) are "chopped" by 9 pixels in the width and become overlapping. the third one remains its regular size.
when spacing is "-1", the width are ok, but there's a 2 pixel spacing between widgets.
help!
-
What about addStretch?
@
QHBoxLayout *layout = new QHBoxLayout;
layout->setSpacing(0);
layout->setContentsMargins(0, 0, 0, 0);
layout->addWidget(new QPushButton("Button1"));
layout->addWidget(new QPushButton("Button2"));
layout->addWidget(new QPushButton("Button3"));
//layout->addSpacerItem(new QSpacerItem(0,0,QSizePolicy::Expanding, QSizePolicy::Expanding));
layout->addStretch(9);
@ -
Adding "addStretch(9)" for some reason makes everything invisible. no buttons are visible at all. and this is the wrong approach. I can hack this by adding the "setSpacing(9)"... but I need to understand why I need to do that. there is another problem on Qt Bug lurking here. Why should I add "9" and not 3.14? why not 2,432,432.12? that's so special about "9"? this needs to be investigated ideally, not hacked around.
-
Solution:
This appears to be a knows Qt problem under OSX. Note the following bug report: http://bugreports.qt.nokia.com/browse/QTBUG-14591
Resolution:
by setting the Qt::WA_LayoutUsesWidgetRect attribute for each button.