Qt page layout

QTlayout overview

 

Qt's layout management system provides powerful mechanisms to automatically arrange all widgets in a window, ensuring that they use space efficiently. Qt includes a set of layout management classes to lay out components in the user interface of the application, such as several subclasses of QLayout, which are called layout managers here. All instances (objects) of subclasses of QWidget can use the layout manager to manage the sub-components located in them. The QWidget : ; setLay-out() function can apply the layout manager on a component. Once a layout manager is set on a widget, it performs several tasks:

The benefits of this article, free to receive Qt development learning materials package, technical video, including (C++ language foundation, C++ design pattern, introduction to Qt programming, QT signal and slot mechanism, QT interface development-image drawing, QT network, QT database programming, QT project actual combat, QSS, OpenCV, Quick module, interview questions, etc.) ↓↓↓↓↓↓See below↓↓Click on the bottom of the article to receive the fee↓↓

(1) Locating subcomponents

(2) Perception window default size

(3) The minimum size of the perception window

(4) Deal with window size changes

(5) Process when the content changes

---- font size, text, or subcomponents

-----Hide or show subcomponents

----Remove child components

layout manager

The QLayout class is the base class of the layout manager. It is an abstract base class that inherits from the QObject and QLayoutItem classes. The QLayoutItem class provides an abstract item for QLayout to operate. Both QLayout and QLayoutItem are used when designing your own layout manager. Generally, you only need to use several subclasses of QLayout. They are QBoxLayout (basic layout manager), QGridLayout (grid layout manager), QFormLayout (window layout manager) and QStackedLayout (stack layout manager).

Basic Layout Manager

The basic layout manager QBoxLayout class can arrange child components in a horizontal or vertical direction, it divides all the space into a row of boxes, and then puts each component into a box. It has two subclasses QHBoxLayout horizontal layout manager and QVBoxLayout vertical layout manager, which are often used in programming. Go back to design mode and look at the properties of the layout manager. First click on the main interface to view its property bar, and the last part is the property of the layout manager it uses

     QHBoxLayout *layout = new QHBoxLayout;      // 新建水平布局管理器
        layout->addWidget(ui->fontComboBox);        // 向布局管理器中添加部件
        layout->addWidget(ui->textEdit);
        layout->setSpacing(50);                     // 设置部件间的间隔
        layout->setContentsMargins(0, 0, 50, 100);  // 设置布局管理器到边界的距离
                                                    // 四个参数顺序是左,上,右,下
        setLayout(layout);                          // 将这个布局设置为MyWidget类的布局

QGridLayout

The grid layout manager QGridLayout class makes components layout in the grid, it divides all the space into some rows and columns, the intersection of rows and columns forms cells, and then puts components into a certain cell . First drag and drop a Push Button on the interface, then add the header file #include<QGridLayout≥ in mywidget.cpp, and then comment out the code about the horizontal layout manager added earlier, which is added as follows

    QGridLayout *layout = new QGridLayout;
        // 添加部件,从第0行0列开始,占据1行2列
        layout->addWidget(ui->fontComboBox, 0, 0, 1, 2);
        // 添加部件,从第0行2列开始,占据1行1列
        layout->addWidget(ui->pushButton, 0, 2, 1, 1);
        // 添加部件,从第1行0列开始,占据1行3列
        layout->addWidget(ui->textEdit, 1, 0, 1, 3);
        setLayout(layout);

This is mainly to set the position of the components in the grid layout manager. Set the fontComboBox component to occupy 1 row and 2 columns, and the pushButton component to occupy 1 row and 1 column. This is mainly to set the length of the fontComboBox component and pushButton component to 2 +1. In this way, if the textEdit component wants to occupy the remaining space, it must make its span be 3 columns. It needs to be explained here that when a component is added to a layout manager, and then the layout manager is placed on a widget, the layout manager and all the components it contains will automatically redefine their own parent object (parent) For this widget, it is not necessary to specify a parent widget when creating a layout manager and its widgets.

form layout

Form layout manager The QFormLayout class is used to manage form input components and their associated labels. The form layout manager divides its child widgets into two columns, some labels on the left, and some input widgets, such as row editors or number selection boxes, on the right. In fact, if it only plays such a layout role, then it can be done with QGridLayout. The reason why the QFormLayout class is added is because it has unique functions.

Vertical Spacer

Creates a gap, placing it between the form layout manager and the horizontal layout manager. Finally, click the main interface and press the shortcut key of Ctrl+L, so that the entire interface is in a vertical layout manager. At this time, you can select the separator Spacer in the object list in the upper right corner, and then set its height to 100 in the property bar, as shown in Figure 4-3. At this time, you can see that the separator is not displayed when you run the program.

set part size

Before explaining, you need to understand two concepts: size hint (sizeHint) and minimum size hint (minimumSize-Hint). All classes inherited from QWidget have these two properties. Among them, the sizeHint property saves the recommended size of the widget. For different widgets, it has different sizeHint by default; and the minimumSizeHint saves a suggested minimum size hint. You can use the sizeHint() function in the program to obtain the value of sizeHint, and use the minimumSizeHint() function to obtain the value of minimumSizeHint. It should be noted that if the minimum size of the widget is set using the setMinimumSize() function, the minimum size prompt will be ignored. These two properties play an important role when using layouts.

scaling factor

I mentioned it when I talked about the vertical layout manager before, but it is actually used to set the ratio between components. The font selection box and a button on the interface are in a horizontal layout manager, and now you want their width ratio to be 2:1, then you can click the horizontalLayout horizontal layout manager object in the object bar, and then in its properties Set the layoutStretch property to "2,1" in the column, so that the width of the two components in this horizontal layout manager is 2:1. If you want to set it in code, you can specify the scaling factor in the second parameter when adding widgets using the layout manager's add Widget() function.

expandable window

A window may have many options to expand, and it will only be displayed when necessary. At this time, a button can be used to hide or display redundant content, which is the so-called expandable window. To realize the expandable window, it is necessary to rely on the characteristics of the layout manager, that is, when the sub-components are hidden, the layout manager will automatically shrink, and when the sub-components are displayed again, the layout manager will be enlarged again.

Combat summary

renderings

 

1. How to realize the expandable window

Applying Signals and Slots

First detect checkable application signal toggled(bool)

code:

void Widget::on_pushButton_toggled(bool checked)
{
    ui->textEdit->setVisible(checked);
    if(checked)ui->pushButton->setText(tr("可隐藏效果"));
    else ui->pushButton->setText(tr("显示可拓展窗口"));
 
}

2. Why is the line length of age not uniform

 

Select expandingfieldjiuxing

The benefits of this article, free to receive Qt development learning materials package, technical video, including (C++ language foundation, C++ design pattern, introduction to Qt programming, QT signal and slot mechanism, QT interface development-image drawing, QT network, QT database programming, QT project actual combat, QSS, OpenCV, Quick module, interview questions, etc.) ↓↓↓↓↓↓See below↓↓Click on the bottom of the article to receive the fee↓↓

Guess you like

Origin blog.csdn.net/m0_73443478/article/details/131601313