2 Attachment(s)
Understanding QLayout, Alignment and (maybe) SizePolicy
Hi all,
so i'm not actually new to Qt, yet I still cannot understand how QLayout, Alignment and Sizepolicy work in combination. Building a complex Layout always results in rage and trial&error.
Its pretty clear that e.g. i will create a QWidget and a QHBoxLayout. To the Layout i will add two PushButtons.
At this point, the button widgets share the layouts width 50:50
Attachment 13622
Changing the buttons Sizepolicy has no effect (probably because the layout[or the button widget itself?] will hintsize here to share the whole size of the layout's widget between the button widgets?).
As soon as I set any Alignment, the button will have a decreased width. I have no idea where that new size is coming from. I thought it might be based on the buttons text but it isnt. SizePolicy still has no effect.
Attachment 13623
I truly dont understand or find any documentation on this behavior.
I can only guess that Qts default hintSize is calculating different for whatever condition.
Can somebody provide a link to a documentation or explain, how those 3 layout objects work together?:confused:
1 Attachment(s)
Re: Understanding QLayout, Alignment and (maybe) SizePolicy
The layout is the only thing that actually sizes the widgets. The layout does that by combining the constraints it has (i.e. the client area of the widget it is attached to) with the demands and suggestions of the widgets that have been added to the layout. The child widgets have a size policy and size hint that express preferences about the sizing of the widget that the layout can use.
In the case of a default button the size policy is Preferred in the horizontal direction and Fixed in the vertical direction. The size hint is driven by the button size needed for the label. In the vertical direction the layout has not resized the buttons to fill the available space precisely because of their size policy. The button horizontal size policy allows for any size, and the layout has stretched them to fit the space available in that direction. The split of space is equal because the two buttons were added to the layout with equal (default) stretch factors.
If you specify alignment of one of the buttons then you are directing the layout to place the widget within the space available to it rather than sizing it to fit. The widget in that case assumes its sizeHint() and is placed, top/bottom left/right etc. of the available space as directed. The space available to each widget does not change with alignment. See the example code and result below (Button 1 and 3 are the same size regardless of the alignment of Button 4)
Code:
#include <QApplication>
#include <QWidget>
#include <QPushButton>
#include <QHBoxLayout>
#include <QVBoxLayout>
int main(int argc, char **argv) {
// Default
// Alignment used
layout2
->addWidget
(new QPushButton("Button 4"),
0, Qt
::AlignHCenter);
layout->addLayout(layout1);
layout->addLayout(layout2);
w.show();
w.resize(800, 150);
return app.exec();
}
Attachment 13624
Your images show a change in size of the left button: without your code I cannot tell you why that is.