Qt layout management

Source: Internet
Author: User
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.

 

  • Qhboxlayout:

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();

 

  • Qvboxlayout:

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.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.