Qt Layout, resize to minimum after widget size changes

Hafnernuss picture Hafnernuss · Feb 20, 2013 · Viewed 33.3k times · Source

Basically I've got a QGridLayout with a few widgets in it. The important ones are 2 labels, which I use for drawing images to the screen. Well, if the user wants, he can change the resolution of the incoming images, thus, forcing the Labels to resize.

Let's assume the initial size of the label is 320x240. The user changes the VideoMode to 640x480, the label and the entire GUI resizes perfectly. But when the user switches back to 320x240, the label shrinks, but the Layout/Window does NOT.

I've played around with sizePolicies and sizeHints, and resize(0,0), but nothing did the trick. Could somebody help me with this?

Here some screenshots to clarify the problem:

Here, everything is okay still, 640 x 480 resizes perfectly but here, the layout is too big.

Answer

user929404 picture user929404 · Jan 30, 2014

You need to set the size constraint of the layout holding all your widgets to "SetFixedSize". Although the name doesn't sound like it will work, it ensures that your layout will only use the space it needs. You will not have the problem like you do in your second screenshot.

Example:

mainLayout.setSizeConstraint(QLayout::SetFixedSize);