(Solved) Automatically resizing layouts

  • I'm creating an application and I use the dockWidgets on both the right and left sides of the programs as conveniently customizable (at runtime) objects. These docks dynamically resize when the window is resized by default. But if I place a layout between them in the main window it wants to have a fixed size.

    Not only is this size too large horizontally to fit between the two dockers in the window's default size, but also when I resize the window at runtime the size of this layout remains the same while the dockers are nicely resizing.

    How can I make the layout and it's contents dynamically expand when I resize the window and make sure that it always fully occupies all space that isn't occupied by docks already?

    I essentially want it to behave "like this":http://qt-project.org/doc/qt-5/qtwidgets-mainwindows-dockwidgets-example.html. but I've been using the forms (.ui) and the designer to create the user interface, and that example doesn't include any ui file so I am a bit confused.

  • Nobody knows this? :(

  • You should (1) set the layout on a widget (you shouldn't just have the layout "floating around"), (2) add some widget(s) to the layout and (3) set the "layout constraints":http://qt-project.org/doc/qt-5/qlayout.html#SizeConstraint-enum and perhaps also the widget(s) "size policies":http://qt-project.org/doc/qt-5/qwidget.html#sizePolicy-prop

    All of these settings can be adjusted from within Designer...I hope this helps.

  • So the way I understand it is that I should add a QWidget to the main window and then right click on it and go to "lay-out" and select the layout I want from there, and then I should add widgets into that, and then I can set the size policies to preferred or expanding and the widget should resize with the window?

    This didn't seem to change anything for me (i.e. the widget size is still fixed and doesn't scale with the window size)

    And just to be sure, the maximum size is set to something ridiculously high (like 16777215x16777215 so that's not the problem)

  • No, not quite...here is a simple test case for you that I hope you can adjust to suit your needs.

    Create a new GUI project, open the form, drag a Dock Widget onto the form and set its dockWidgetArea to RightDockWidgetArea, drag another onto the form and set its dockWidgetArea to LeftDockWidgetArea (you now should have two dock widgets, one on either side of your main window form). Drag a frame onto your form, you will notice that the Layout Icons (just above the form in the Designer view) are now active. Select e.g. a horizontal layout (this sets the layout on the main window's central widget and adds the frame to the central widget's layout).

    This has the exact behaviour you are looking for (as per my understanding of your explanation).

  • OK, here is some code for what I described above: "test":https://www.dropbox.com/sh/dhptllrbcrvixg8/AAAZ5UHCHs82lfAn1ocXR1KSa?dl=0

    Play around with it a bit, there is nothing fancy to what I have done here, I literally only executed the steps as described in my previous post.

  • Hmm this worked as you said when I created a new project, however in my current project for reasons unknown to me it won't work the same way. I think it has to do with this stop sign on "centralWidget"

    Aha! yes there we go, that was the problem. My centralWidget was for some reason configured to "Break Layout" that was why the frames and layouts in there weren't resizing on my original project.

    Thanks for your help :)

  • No problem, glad you figured it out! I have had my fair amount of struggling with layouts and resizing :D

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.