Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Qt Layout, resize to minimum after widget size changes

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 okaystill, 640 x 480 resizes perfectlybut here, the layout is too big.

like image 844
Hafnernuss Avatar asked Feb 20 '13 13:02

Hafnernuss


People also ask

How do I change the size of a widget in Qt?

If the width won't change afterwards, you can use setFixedWidth : widget->setFixedWidth(165); Similarly, to change the height without changing the width, there is setFixedHeight . Never use fixed width!

How do I resize a layout in Qt?

Once you have add your layout with at least one widget in it, select your window and click the "Update" button of QtDesigner. The interface will be resized at the most optimized size and your layout will fit the whole window. Then when resizing the window, the layout will be resized in the same way.

What is stretch factor Qt?

Stretch Factors When they are laid out in a layout the widgets are given a share of space in accordance with their QWidget::sizePolicy() or their minimum size hint whichever is the greater. Stretch factors are used to change how much space widgets are given in proportion to one another.


1 Answers

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); 
like image 94
user929404 Avatar answered Oct 06 '22 22:10

user929404