Qt Widget layout management system

Insert picture description here

Qt's layout management system provides a powerful mechanism to automatically arrange the components in the window to ensure that they use space effectively. Several subclasses of QLayout, here are called layout managers. All instances (objects) of subclasses of QWidget can use the layout manager to manage the child widgets located in them, and the QWidget::setLayout() function can apply the layout manager to a widget. Once the layout manager is set on a widget, it will complete the following tasks:

  • Positioning sub-components

  • Perception window default size

  • Minimum size of perception window

  • Process when the window size changes

  • Automatically update when content changes

    • The font size, text or other content of the sub-component will change accordingly
    • Hide or show subcomponents
    • Remove a subcomponent

The QLayout class is the base class of the layout manager. It is an abstract base class inherited from the QObject and QLayoutItem classes. The QLayoutItem class provides an abstract item for QLayout operations. Both QLayout and QLayoutItem are used when designing their own layout managers. Generally, only a few subclasses of QLayout are needed. They are QBoxLayout (basic layout manager) and QStackedLayout (stack layout manager).

Basic layout manager (QBoxLayout)

The basic layout manager QBoxLayout class can arrange the sub-components in a row in the horizontal or vertical direction. It divides all the space into a row of boxes, and then puts each component in a box.

  • QHBoxLayout (Horizontal Layout Manager)
  • QVBoxLayout (Vertical Layout Manager)
#include <QHBoxLayout>
	
QHBoxLayout *layout = new QHBoxLayout;

// 正确用法是要实例化控件对象后使用
// 向布局中添加控件
layout->addWidget(QPushButton);
layout->addWidget(QLabet);

// 设置部件间的间隔
layout->setSpacing(50);

// 设置布局管理器到边界的距离
layout->setContentsMargins(0,0,50,100);

// 将这个布局设置为this的布局
setLayout(layout);


Grid Layout Manager (QGridLayout)

The grid layout manager QGridLayout class makes the components lay out in the grid. It divides all the space into rows and columns. The intersection of the rows and columns forms a cell, and then puts the components into a certain cell. .

QGridLayout *layout = new QGridLayout;

// 添加部件,从第0行0列开始,占据1行2列
layout->addWidget(QWidget, 0,0,1,2);

setLayout(layout);

Form layout manager (QFormLayout)

The form layout manager (QFormLayout) class is used to manage the input parts of the form and the labels associated with them.

Guess you like

Origin blog.csdn.net/qq_32312307/article/details/115048209