当前位置:网站首页>Qt学习23 布局管理器(二)
Qt学习23 布局管理器(二)
2022-07-03 13:23:00 【一个小黑酱】
Qt学习23 布局管理器(二)
布局管理器
- 布局管理器中的 比例系数
- 默认情况下以等比例的方式 更新组件的大小
- 可以自定义 组件大小更新 比例系数
- QBoxLayout 中的比例系数设置
- void setStretch(int index, int stretch)
- bool setStretchFactor(QWidget* widget, int stretch)
- bool setStretchFactor(QLayout* layout, int stretch)
编程实验 - 比例系数初探
上一节代码中添加设置比例系数功能
void Widget::testVBoxLayout()
{
// 布局管理器添加窗口控件
// 布局管理器设置控件间的间距
// 设置比例系数
layout->setStretch(0, 1);
layout->setStretch(1, 2);
layout->setStretch(2, 3);
layout->setStretch(3, 4);
// 设置当前窗口的布局管理器
}
void Widget::testHBoxLayout()
{
// 布局管理器添加窗口控件
// 布局管理器设置控件间的间距
// 设置比例系数
layout->setStretchFactor(&TestBtn1, 1);
layout->setStretchFactor(&TestBtn2, 3);
layout->setStretchFactor(&TestBtn3, 1);
layout->setStretchFactor(&TestBtn4, 4);
// 设置当前窗口的布局管理器
}
void Widget::testVHBoxLayout()
{
// ...
vLayout->setStretchFactor(hLayout1, 1);
vLayout->setStretchFactor(hLayout2, 2);
// ...
}
- 组件的初始大小是 独立于布局管理器 设置的,因此 不能保证组件的大小始终符合比例系数的设置
QGridLayout布局管理器
- 以 网格(二维) 的方式管理界面组件

- QGridLayout 中的比例系数设置
- void setColumnStretch(int column, int stretch)
- void setRowStretch(int row, int stretch)
编程实验 - QGridLayout
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include <QPushButton>
class Widget : public QWidget
{
Q_OBJECT
private:
QPushButton TestBtn1;
QPushButton TestBtn2;
QPushButton TestBtn3;
QPushButton TestBtn4;
void initBtn();
void testGridLayout1();
void testGridLayout2();
public:
Widget(QWidget *parent = 0);
~Widget();
};
#endif // WIDGET_H
#include "Widget.h"
#include <QGridLayout>
Widget::Widget(QWidget *parent) : QWidget(parent),
TestBtn1(this), TestBtn2(this), TestBtn3(this), TestBtn4(this)
{
initBtn();
// testGridLayout1();
testGridLayout2();
}
Widget::~Widget()
{
}
void Widget::initBtn()
{
TestBtn1.setText("Test Button 1");
TestBtn1.setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
TestBtn1.setMinimumSize(160, 30);
TestBtn2.setText("Test Button 2");
TestBtn2.setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
TestBtn2.setMinimumSize(160, 30);
TestBtn3.setText("Test Button 3");
TestBtn3.setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
TestBtn3.setMinimumSize(160, 30);
TestBtn4.setText("Test Button 4");
TestBtn4.setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
TestBtn4.setMinimumSize(160, 30);
}
void Widget::testGridLayout1()
{
QGridLayout* layout = new QGridLayout();
layout->setSpacing(10);
// 栅格布局管理添加按钮组件
layout->addWidget(&TestBtn1, 0, 0);
layout->addWidget(&TestBtn2, 0, 1);
layout->addWidget(&TestBtn3, 1, 0);
layout->addWidget(&TestBtn4, 1, 1);
// 设置行比例系数
layout->setRowStretch(0, 1);
layout->setRowStretch(1, 2);
// 设置列比例系数
layout->setColumnStretch(0, 3);
layout->setColumnStretch(1, 4);
setLayout(layout);
}
void Widget::testGridLayout2()
{
QGridLayout* layout = new QGridLayout();
layout->setSpacing(10);
// 栅格布局管理跨栅格添加按钮组件
layout->addWidget(&TestBtn1, 0, 0, 2, 1);
layout->addWidget(&TestBtn2, 0, 1, 2, 1);
layout->addWidget(&TestBtn3, 2, 0, 1, 2);
layout->addWidget(&TestBtn4, 3, 0, 1, 2);
setLayout(layout);
}
#include "Widget.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Widget w;
w.show();
return a.exec();
}
- 布局管理器的嵌套
- QGridLayout 支持 嵌套其他布局管理器 成为其管理对象

小结
- QGridLayout 以 网格的方式 对组件进行管理
- QGridLayout 中的组件可以 根据需要跨越多个网格
- QBoxLayout 和 QGridLayout 支持 比例系数 的概念
- 比例系数 决定了组件大小的 相对变化
边栏推荐
- Unity embeddedbrowser browser plug-in event communication
- TensorBoard可视化处理案例简析
- The solution of Chinese font garbled code in keil5
- 研发团队资源成本优化实践
- 树的深入和广度优先遍历(不考虑二叉树)
- Father and basketball
- 使用tensorflow进行完整的DNN深度神经网络CNN训练完成图片识别案例
- 记录关于银行回调post请求405 问题
- Golang - command line tool Cobra
- [how to earn a million passive income]
猜你喜欢

Kivy教程之 如何自动载入kv文件

Another industry has been broken by Chinese chips. No wonder the leading analog chip companies in the United States have cut prices and sold off

Complete deep neural network CNN training with tensorflow to complete picture recognition case 2

Kivy tutorial how to automatically load kV files

Mycms we media mall v3.4.1 release, user manual update

Go language web development series 25: Gin framework: using MD5 to verify the signature for the interface station
![[redis] cache warm-up, cache avalanche and cache breakdown](/img/df/81f38087704de36946b470f68e8004.jpg)
[redis] cache warm-up, cache avalanche and cache breakdown

AI 考高数得分 81,网友:AI 模型也免不了“内卷”!

MySQL 数据增删改查综合案例

Can newly graduated European college students get an offer from a major Internet company in the United States?
随机推荐
pytorch 载入历史模型时更换gpu卡号,map_location设置
When updating mysql, the condition is a query
Can newly graduated European college students get an offer from a major Internet company in the United States?
Resolved (error in viewing data information in machine learning) attributeerror: target_ names
Go language unit test 5: go language uses go sqlmock and Gorm to do database query mock
MySQL installation, uninstallation, initial password setting and general commands of Linux
实现CNN图像的识别和训练通过tensorflow框架对cifar10数据集等方法的处理
[développement technologique - 24]: caractéristiques des technologies de communication Internet des objets existantes
GoLand 2021.2 configure go (go1.17.6)
如何使用lxml判断网站公告是否更新
Mysql:insert date:SQL 错误 [1292] [22001]: Data truncation: Incorrect date value:
Golang — template
Shell timing script, starting from 0, CSV format data is regularly imported into PostgreSQL database shell script example
[556. Next larger element III]
树的深入和广度优先遍历(不考虑二叉树)
Mycms we media mall v3.4.1 release, user manual update
Go language web development series 27: Gin framework: using gin swagger to implement interface documents
[bw16 application] instructions for firmware burning of Anxin Ke bw16 module and development board update
TensorBoard可视化处理案例简析
8 Queen question