当前位置:网站首页>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 支持 比例系数 的概念
- 比例系数 决定了组件大小的 相对变化
边栏推荐
- Father and basketball
- Go language web development series 30: gin: grouping by version for routing
- User and group command exercises
- 3D视觉——2.人体姿态估计(Pose Estimation)入门——OpenPose含安装、编译、使用(单帧、实时视频)
- Logback log sorting
- Disruptor -- a high concurrency and high performance queue framework for processing tens of millions of levels
- Complete DNN deep neural network CNN training with tensorflow to complete image recognition cases
- Setting up remote links to MySQL on Linux
- Replace the GPU card number when pytorch loads the historical model, map_ Location settings
- HALCON联合C#检测表面缺陷——HALCON例程autobahn
猜你喜欢

GoLand 2021.1: rename the go project
![[bw16 application] instructions for firmware burning of Anxin Ke bw16 module and development board update](/img/b8/31609303fd817c48b6fff7c43f31e5.png)
[bw16 application] instructions for firmware burning of Anxin Ke bw16 module and development board update

Ocean CMS vulnerability - search php

挡不住了,国产芯片再度突进,部分环节已进到4nm

The shortage of graphics cards finally came to an end: 3070ti for more than 4000 yuan, 2000 yuan cheaper than the original price, and 3090ti

SQL Injection (GET/Select)

GoLand 2021.1.1: configure the multi line display of the tab of the open file

This math book, which has been written by senior ml researchers for 7 years, is available in free electronic version

【电脑插入U盘或者内存卡显示无法格式化FAT32如何解决】

Use and design of Muduo buffer class
随机推荐
Mycms we media mall v3.4.1 release, user manual update
Unable to stop it, domestic chips have made another breakthrough, and some links have reached 4nm
Go language web development series 25: Gin framework: using MD5 to verify the signature for the interface station
This math book, which has been written by senior ml researchers for 7 years, is available in free electronic version
mysql中的字段问题
Universal dividend source code, supports the dividend of any B on the BSC
Father and basketball
JSON serialization case summary
When updating mysql, the condition is a query
Go language unit test 5: go language uses go sqlmock and Gorm to do database query mock
常见的几种最优化方法Matlab原理和深度分析
Error handling when adding files to SVN:.... \conf\svnserve conf:12: Option expected
[how to earn a million passive income]
NFT new opportunity, multimedia NFT aggregation platform okaleido will be launched soon
The latest BSC can pay dividends. Any B usdt Shib eth dividend destruction marketing can
3D视觉——2.人体姿态估计(Pose Estimation)入门——OpenPose含安装、编译、使用(单帧、实时视频)
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
Go: send the get request and parse the return JSON (go1.16.4)
Field problems in MySQL
AI 考高数得分 81,网友:AI 模型也免不了“内卷”!