Search code examples
qtuser-interfaceqlayout

Creating auto-scaling Qt UI using Layouts


I'd appreciate advise on to how create a Qt UI consisting of four layouts and has the following properties.

  1. Any increases in the height of the UI is absorbed by the layouts, as shown below enter image description here

  2. Any further decreases in the height of the UI is absorbed by the contents of a specific layout, e.g. the two large buttons as shown below enter image description here


Solution

  • I prepared quick code what fits your problem, please take a look: https://github.com/troyane/StackOverflow-pro/tree/master/creating-auto-scaling-qt-ui-using-layouts

    Grab that code and take a look at next moments (you can open mainwindow.ui in QtCreator):

    • centralWidget has next layoutStretch param: 1,2,1,1 -- it means, that we'll have next correlation among all items placed into this vertical layout.
    • TextLabel and both SmallButtons has Fixed VerticalPolicy
    • Both BigButtons has Minimum vertical policy and set minimumSize's Height to 100. UPD: Also maximumSize->height parameter is 250 px. So, it is guarantee that both BigButtons will not grow more than 250 px on height.

    Take a look at another answer, there you can find lots of literature to read about Layouts.