Resize behavior in Qt layouts

I want my custom widgets to get extra space when resizing a dialog box. This worked when I only had a few widgets, but after adding several columns of the same widgets and placing them in a QGridLayout, the extra space just comes in as an addition between the widgets.

+4
source share
3 answers

I had problems with this in the past, and here are some of the things I found:

  • First, make sure that all the widgets you want to expand have a Politicy size set to "Expanding".

  • Make sure the widgets that make up your own widgets are in a layout that allows you to expand. You can verify this by simply adding one of your custom widgets to the window and seeing that it expands as expected.

  • Make sure that all widgets in the form that you do not want to expand have a fixed (minimum = maximum) size in the dimension in which you want them to remain static.

  • Sometimes the grid layout causes some strange spacing problems because the rows change based on the largest widget in the entire row and similarly for columns. For some layouts, it’s best to use a vertical layout that contains horizontal layouts, or vice versa, to create a mesh effect. Only in this way, each sub-layout is spaced independently of other rows or columns.

+11
source

Grid Extension Management

I found that you can easily control which columns / rows expand and which columns / rows remain fixed in width using QGridLayout::setColumnStretch() and QGridLayout::setRowStretch() . You will need to provide weights for specific columns (0 without stretch marks).

For example, if you want column 0 to occupy no room and column 1 to take the rest of the window room, do the following:

 QGridLayout* layout ; // Set up the layout layout->setColumnStretch( 0, 0 ) ; // Give column 0 no stretch ability layout->setColumnStretch( 1, 1 ) ; // Give column 1 stretch ability of ratio 1 

Controlling grid extension with Qt Designer

You can do what I described above if you use the constructor. Just find the widget properties layoutRowStretch and layoutColumnStretch. It will contain a list of integers, separated by commas.

+8
source

Another option inside QT Creator is to specify in the top-level widgets of the section that you want the fixed size to be layoutSizeConstraint from "SetFixedSize". You must also remove all spacers from under this widget. In my case, I had a dialog with a TreeWidget, a table, and some color pickers. I wanted the color pickers to remain the same horizontal size, so they were in VerticalLayout. I think you can do the same with HorizontalLayout if you want things to stay the same height. If you really need spacers inside the layout, you can probably use blank labels with a fixed size.

0
source

All Articles