View Full Version : Understanding QSizePolicy

17th September 2012, 23:53

The more I try to understand how QSizePolicy works, I fail. I have for example a QLabel and a QListWidget at QtDesigner. I put them into a QSplitter (horizontal) and I want the QLabel expands as much as posible and the QListWidgets don't shrinks more than 100 in its height. So I choose the vertical QSizePolicy of the QLabel to Expanding and the vertical QSizePolicy of the QListWidget to Minimum (and Maximum, and all combinations) and also fixing the Minimum size to 100. Well, the applications does what he wants and the QLabel shrinks the maximum and the QListWidgets grows as he wants. I also tried all the combinations between size policies and it still fails. Furthermore, I don't want to set the minimum size and the maximum size to the same fixed value because I want that user controls the size of the widgets. I only want that at the app starts, the QLabel uses all the possible space and the QListWidget shrinks at size 100.

Thanks in advance.

18th September 2012, 03:40
I don't know how it works with a splitter but if you use one of the layouts, this details you have just mentioned can be controlled with stretches; this can be added either using a separate function or simply providing a second parameter in addWidget / addLayout. For instance, I want to divide a window into two horizontal regions. I want a bottom region to stay fixed whereas the upper one to be rescaled when the window is rescaled.

QVBoxLayout* vBox = new QVBoxLayout;
vBox->addWidget(new QLabel("Label 1"), 1);
vBox->addWidget(new QListWidget);

By default, QLabel will probably flow but in the middle but when you set QSizePolicy to Maximum it may be rescaled as well. You may also want the upper part to be stretched twice as much as the bottom one. Then you can coded this as follows

vBox->addWidget(new QLabel("Label 1"),2);
vBox->addWidget(new QListWidget,1);

I cannot test this code so I cannot be 100% sure it will work but it may help you figure out something. Also, there is a good tutorial about how to use Layouts.

18th September 2012, 20:07
Yes, it seems to work halfway but cannot get the behaviour I want. I do not know what to do, I can not understand the behavior of size Policies, etc. Thanks for the help.