QT Layout - Starting Directions

I am new to QT. I am trying to understand the layout mechanism by trying to implement this small window, which can be seen below. It has the following elements under QWidget, which is the main window:

  • One big QWidget that spans the entire client area.
  • Two QWidget containers at the top of the window. Both should have the same height, but the right section extends horizontally as the window grows / shrinks.
  • container widget with one button in the upper right corner with a fixed height and width
  • A large QWidget container that fills the rest of the client area, which should change when the window is resized.

The parent window itself can be changed.

I am looking for clues as to which layout I should use. How can I achieve this programmatically? determine what stretches automatically, what remains with the size of the patch? and how proportions are maintained where they need to be maintained.

I would be grateful for any pointer that you have.

What I'm trying to achieve

+4
source share
2 answers

The easiest and best way for IMHO is to do it through QHBoxLayout and QVBoxLayouts. You can do this through the designer in QtCreator, but I believe that it does not work perfectly if you need to adapt over time. If this is a static set of widgets, I suggest creating it using the QtCreator constructor, as it will greatly simplify your life.

If you are going to do this programmatically, the main window should be configured to use QVBoxLayout, and then two sub-QVBoxLayout after that, where the bottom is configured to receive any space that it can receive. Then in the top QVBoxLayout add a QHBoxLayout with the top two components.

+3
source

to set the widget to a fixed size in the code that you call setFixedSize (int h, int w) in the widgets. To do this in Designer, click on the widget and look in the property editor in the QWidget section. open the sizePolicy thingy and set the horizontal and / or vertical value to fixed. Then open the Geometry and set the width and height.

To make them stretched with different coefficients in the code, you use a separate argument when using the window layout. e.g. layout-> addWidget (button1, 1); layout-> addWidget (button2, 2); this will cause button 2 to expand at twice the speed of button1. To do this in the designer, open the widgets sizePolicy property and set HorizontalStrech and / or VerticalSretch. Please note that in this case, the size policy should not be fixed in the direction in which you want to configure stretching. In addition, it will never allow a widget to be smaller than its minimum size (this would ruin the ratio rather than reduce something too small).

+3
source

Source: https://habr.com/ru/post/1337981/


All Articles