Search code examples
qtwidgetqgridlayout

Qt: Why children widgets overlapping in QGridLayout?


I have an widget x with fixed size. Then at first I took a QScrollArea and then a QGridLayout which I set as the layout of scrollAreaWidgetContent. Then I started adding some widget x in the layout at (0,0), (0,1), (1,0), (1,1)... of grid layout. Then when I ran the program, strangely when I shrink the window vertically, the widgets overlap.

Overlaped widgets

And as expected, when I increase the window size vertically, they are not overlapped anymore.

Non overlapped

But strange thing is this problem does not occurring when I shrink window horizontally. For example,

Horizontal resizing

My question is, why this is happening and more importantly, keeping in mind I want to use gridlayout, how to solve this problem?


Solution

  • As you said in your question: Your widget has a fixed size, so the layout does not shrink them when there is no space left. So the question is: what do you want to happen when you shrink the window?

    1. If you want to shrink your widget you have to change the sizePolicy of your widgets from "Fixed" to "Preferred".
    2. If you want to reduce the number of widgets in the layout, then you could add an event handler in the resize event and remove them
    3. If you want to disallow the shrinking of the window, then you need to set the sizePolicy of the scrollarea to a fixed or minimumSize