Horizontal, vertical, grid layouts
First, introduce the most simple layout configuration method, that is, use the basic layout configuration management provided by QT: qhboxlayout, qvboxlayout and
Qgridlayout.
These types must be different from the qlayout type (and then the qobject type ). These types of widgets are mainly used to manage the layout of widgets.
Configuration process
You can create more complex layout configurations.
The following program creates a qhboxlayout and places five qpushbutton in it. The result is shown in the following figure:
QWidget *window = new QWidget;
QPushButton *button1 = new QPushButton("One");
QPushButton *button2 = new QPushButton("Two");
QPushButton *button3 = new QPushButton("Three");
QPushButton *button4 = new QPushButton("Four");
QPushButton *button5 = new QPushButton("Five");
QHBoxLayout *layout = new QHBoxLayout;
layout->addWidget(button1);
layout->addWidget(button2);
layout->addWidget(button3);
layout->addWidget(button4);
layout->addWidget(button5);
window->setLayout(layout);
window->show();
The qvboxlayout program is similar to the above qhboxlayout. The result of its production is as follows:
- Qgridlayout arranges widgets into two-dimensional grids. widgets can contain multiple grids:
The use of qgridlayout is slightly different, because the row and column location of the widget must be specified.
QWidget *window = new QWidget;
QPushButton *button1 = new QPushButton("One");
QPushButton *button2 = new QPushButton("Two");
QPushButton *button3 = new QPushButton "Three");
QPushButton *button4 = new QPushButton("Four");
QPushButton *button5 = new QPushButton("Five");
QGridLayout *layout = new QGridLayout;
layout->addWidget(button1, 0, 0);
layout->addWidget(button2, 0, 1);
layout->addWidget(button3, 1, 0, 1, 2);
layout->addWidget(button4, 2, 0);
layout->addWidget(button5, 2, 1);
window->setLayout(layout);
window->show();
The third qpushbutton contains two rows, so the fifth row is specified in qgridlayout: addwidget.
Use this layout configuration,
When you configure a child widget layout, you do not need to introduce the parent widget layout data. This layout configuration automatically uses qwidget: setparent () to import the parent widget.
When a user uses a layout to apply to this window, and adds other widgets (such as Pushbutton in the above example) to this layout, these widgets are automatically
The child widget of layout is used by the application in the window. (Remember, Widgets cannot inherit layout, but only widgets can ).
In addition, you can use addlayout () to add other layout configurations to the layout configuration. The layout configuration added to addlayout () becomes the child of the layout configuration,
To form a more complex layout.