QT:水平布局和垂直布局

在这里插入图片描述
QVBoxLayout:垂直布局

QWidget *widget = new QWidget();
QVBoxLayout * vBoxLayout = new QVBoxLayout();
QLabel *label = new QLabel("hello");
QPushButton *pushButton = new QPushButton("hi");
vBoxLayout->addWidget(label);
vBoxLayout->addWidget(pushButton);
widget->setLayout(vBoxLayout);
widget->show();

在这里插入图片描述
QHBoxLayout:水平布局
代码实现:

QWidget *widget = new QWidget();
QHBoxLayout * hBoxLayout = new QHBoxLayout();
QLabel *label = new QLabel("hello");
QPushButton *pushButton = new QPushButton("hi");
hBoxLayout->addWidget(label);
hBoxLayout->addWidget(pushButton);
widget->setLayout(hBoxLayout);
widget->show();```
![在这里插入图片描述](https://img-blog.csdnimg.cn/20210205161753978.png)
**QHBoxLayout嵌套QVBoxLayout**
代码实现:

```cpp
QWidget *widget = new QWidget();
QHBoxLayout * hBoxLayout = new QHBoxLayout();
QVBoxLayout * vBoxLayout = new QVBoxLayout();
QLabel *label_text = new QLabel("Chengdu 1 – 3 Day Weather Forecast Summary");
QLabel *label_picture = new QLabel();
label_picture->setPixmap(QPixmap(":/weather").scaled(32,32, Qt::KeepAspectRatioByExpanding));
QPushButton *pushButton = new QPushButton("more");
vBoxLayout->addWidget(label_text);
vBoxLayout->addWidget(pushButton);
hBoxLayout->addWidget(label_picture);
hBoxLayout->addLayout(vBoxLayout);
widget->setLayout(hBoxLayout);
widget->show();

在这里插入图片描述


布局使用小结:
1、向布局中添加一个Widget

void QBoxLayout::addWidget(QWidget *widget, int stretch = 0, Qt::Alignment alignment = Qt::Alignment())

2、向布局中添加一个布局

void QBoxLayout::addLayout(QLayout *layout, int stretch = 0)

3、确定Widget的布局

void QWidget::setLayout(QLayout *layout);

转载自:
作者:静_谷
链接:https://www.jianshu.com/p/938301a4d54e
来源:简书


下面附加一个联系的示例,供参考学习:
链接:
https://blog.csdn.net/zq9955/article/details/113698935
在这里插入图片描述

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值