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;
// add before the first button
layout->addStretch();
layout->addWidget(button1);
layout->addStretch();
layout->addWidget(button2);
layout->addStretch();
layout->addWidget(button3);
layout->addStretch();
layout->addWidget(button4);
layout->addStretch();
layout->addWidget(button5);
//add scaling at the last
layout->addStretch();
//Set the margin to 10
layout->setMargin(20);
//set the spacing to 0
//layout->setSpacing(0);
window->setLayout(layout);
window->show();
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
twenty one.
twenty two.
twenty three.
twenty four.
25.
26.
27.
28.
29.
30.
Results of the:
4.4 Adding controls
addWidget(QWidget *, int stretch = 0, Qt::Alignment alignment = 0)
By default, we add controls to the horizontal layout, which are centered vertically by default.
1.
2.
It will be obvious when some of the controls are of different sizes. If we need to display some of the controls on the top and bottom, we can use the alignment method Qt::Alignment.
QWidget *window = new QWidget;
window->resize(320, 128);
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;
// add before the first button
// horizontally to the left, vertical to the top
layout->addWidget(button1, 0, Qt::AlignLeft | Qt::AlignTop);
layout->addWidget(button2, 0, Qt::AlignLeft | Qt::AlignTop);
layout->addWidget(button3);
// horizontally to the left, vertical to the bottom
layout->addWidget(button4, 0, Qt::AlignLeft | Qt::AlignBottom);
layout->addWidget(button5, 0, Qt::AlignLeft | Qt::AlignBottom);
//Set the margin to 10
layout->setMargin(20);
//set the spacing to 10
layout->setSpacing(10);
window->setLayout(layout);
window->show();
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
twenty one.
twenty two.
twenty three.
twenty four.
25.
26.
27.
Results of the:
4.5 Set the layout direction
setDirection(Direction)
Set layout direction
You can set from left to right, right to left, top to bottom, bottom to top, etc. . .
setDirection(QBoxLayout::RightToLeft)
setDirection(QBoxLayout::TopToBottom);
1.
2.
3.
4.
5.
6.
7.
8.
Program example:
QWidget *window = new QWidget;
window->resize(320, 128);
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);
//Set the margin to 10
layout->setMargin(20);
//set the spacing to 10
layout->setSpacing(10);
//set the layout direction
layout->setDirection(QBoxLayout::TopToBottom);
window->setLayout(layout);
window->show();
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
twenty one.
twenty two.
twenty three.
twenty four.
25.
26.
27.
28.
Results of the:
Since QHBoxLayout is used, it is generally not recommended to use TopToBottom or BottomToTop. If the direction cannot be determined, or the direction can be changed at will, it is recommended to use QBoxLayout.
4.6 Setting the stretch factor
setStretchFactor(QWidget *w, int stretch);
setStretchFactor(QLayout *l, int stretch);
Set the stretch factor for controls and layouts
When the size of the form changes, the control will adjust accordingly according to the stretch factor.
1.
2.
3.
4.
setStretchFactor(pButton1, 1);
setStretchFactor(pButton2, 2);
Set the stretch coefficient of pButton1 to 1, and the stretch coefficient of pButton2 to 2. When the form becomes larger, pButton2 will be stretched first. When it reaches a certain level, pButton1 will be stretched again. The width ratio of pButton1 to pButton2 is 1. :2.
Program example:
QWidget *window = new QWidget;
window->resize(320, 128);
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);
//Set the margin to 10
layout->setMargin(20);
//set the spacing to 10
layout->setSpacing(10);
layout->setStretchFactor(button1, 1);
layout->setStretchFactor(button2, 2);
window->setLayout(layout);
window->show();
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
twenty one.
twenty two.
twenty three.
twenty four.
25.
26.
27.
Results of the:
05. Vertical layout
The vertical layout and the horizontal layout are all the same except for the orientation.
-----------------------------------
© Copyright belongs to the author: original works from 51CTO blogger itcast0, please contact the author for reprint authorization, otherwise legal responsibility will be pursued
[Qt] Horizontal and vertical layout
https://blog.51cto.com/dlican/3740122
Contact: Manager Xu
Phone: 13907330718
Tel: 0731-22222718
Email: hniatcom@163.com
Add: Room 603, 6th Floor, Shifting Room, No. 2, Orbit Zhigu, No. 79 Liancheng Road, Shifeng District, Zhuzhou City, Hunan Province