Olumide
Olumide

Reputation: 5819

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

Upvotes: 1

Views: 5956

Answers (3)

NG_
NG_

Reputation: 7183

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.

Upvotes: 1

Blueman
Blueman

Reputation: 811

1# Create new UI form base on QWidget: File -> New file or project -> Qt -> Qt Designer form class -> select Widget form templates, next, next, select project and finish

2# Add Vertical layout from left bar

enter image description here

3# After that click right somewhere on UI form, where is not just added layout, "Lay out" -> "Lay out in a grid"

enter image description here

4# You can adjust layout margin on right menu (I'm always setting 5 points)

5# Add four Horizontal layout

enter image description here

6# Add Button and text and what you need

enter image description here

7# Add Vertical spacers between Horizontal layout

enter image description here

8# Final result:

enter image description here enter image description here enter image description here

Upvotes: 9

Nejat
Nejat

Reputation: 32655

You can put two vertical spacers in each layout. One should be placed at top most and the other one at the bottom :

enter image description here

Upvotes: 0

Related Questions