Important: Please read the Qt Code of Conduct -

Layout - adjust size to its content

  • How can I force my layout to adjust its size as per the child widgets. What I have is a vertical layout on my QMainWidget. I am adding sub widgets to it based on some conditions. I need my vertical layout to shrink and expand based on the number of widgets I add to my vertical layout.

  • There is showcase for the same. See

  • I am not sure if that is what I need.. I saw the video, there the size of the layout is same and child widgets are adjusted to available space.

    What I am looking for is if my layout has two widget with 100 px by 100 px , by layout should adjust to size 100px by 200 px, and if I have three widget my layout should adjust to size 100px by 300 px

  • You can do that, by making sure the widgets you put in that layout have a minimum size.

  • Use QDesktopWidget::screenGeometry to get the screen size and then you can decide based on the ratio you want for your widgets. You can then use QLayout::SetFixedSize in case you want the layout to be of exactly fixed size.

  • I was hoping I can use the sizing policy for layout to get this. but it does not seems to work for me.

Log in to reply