qtqt-designerqlayout

Qt layout is larger than it should be


I had a layout all nicely designed in Qt, but as soon as I clicked on the parent window and set it to a grid layout, things got all wonky:

enter image description here

As you can see, the vertical layout on the left insists on being wider than the children it contains. Both the label and the treeview are set to sizePolicy Maximum, and the maximum width is set to 260px. The children themselves stay the correct size, but the vertical layout that contains them doesn't.

The vertical layout in the middle is set to expanding, and the one on the far right is set up the same as the one on the left, only that one appears to work. How do I make the first vertical layout conform to its children's width?


Solution

  • The problem is most likely that you need to experiment with "stretching" the layout. Stretch sets the size of layout cells in relation to each other. The default is 0, which means no stretching occurs.

    In your case, I believe you want to set the stretch of the first column (column 0) to 0, and the stretch of the second and third columns to 1. This means that the first column will always be as small as possible, and the second and third columns will try to be equally wide.

    You can set the stretch programmatically quite easily; for example, to set the first column to stretch 0:

    layout->setColumnStretch(0, 0);
    

    In Qt Designer you can access column and row stretches as any normal properties.