4

I want my custom widgets to gain extra space when the dialog is resized. This was working when I only had a handful of widgets, but after adding several more columns of these same widgets and putting them in a QGridLayout, the extra space merely goes in as padding between the widgets.

3 Answers 3

18

I've had trouble with this in the past and here are some of the things I've found:

  1. First make sure all the widgets you want to expand have sizePolicy set to "Expanding".

  2. Make sure the widgets that make up your custom widgets are in a layout that allows for expanding. You can check this by just adding one of your custom widgets to the window and seeing that it expands as expected.

  3. Make sure any widgets on the form that you do not want to expand have a fixed (minimum=maximum) size in the dimension you want them to stay static.

  4. Sometimes the grid layout causes some weird spacing issues because rows are resized based on the largest widget in the entire row and similarly for columns. For some layouts, it is better to use a vertical layout that contains horizontal layouts or vica versa to create a grid-like effect. Only this way, each sub-layout is spaced independently of the other rows or columns.

0
12

Controlling grid expansion programatically

I've found that you can easily control which columns/rows expand and which columns/rows stay fixed in width by using QGridLayout::setColumnStretch() and QGridLayout::setRowStretch(). You'll need to provide weights to the specific columns (0 for no stretch).

For example, if you want column 0 to not take up any room and column 1 to take the rest of the window's room, do this:

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 expansion using Qt Designer

You can do what I described above if you're using Designer. Just look for the widget properties layoutRowStretch and layoutColumnStretch. It'll contain a comma-separated list of integers.

0

Another option is inside of QT Creator, to specify in the top level layout widget of the section you want fixed size a layoutSizeConstraint of "SetFixedSize". You must also remove all spacers from beneath that widget. In my case, I had a dialog with a TreeWidget, a Table, and some color selection stuff. I wanted the color selection controls to remain the same size horizontally, so they were in a VerticalLayout. I imagine you can do the same with a HorizontalLayout too if you want things to stay the same height. IF you really need spacers inside the layout, you can probably use blank labels with fixed size.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.