当前位置:网站首页>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 支持 比例系数 的概念
- 比例系数 决定了组件大小的 相对变化
边栏推荐
- Unable to stop it, domestic chips have made another breakthrough, and some links have reached 4nm
- JVM系列——概述,程序计数器day1-1
- The network card fails to start after the cold migration of the server hard disk
- SQL Injection (POST/Select)
- Use docker to build sqli lab environment and upload labs environment, and the operation steps are provided with screenshots.
- Record 405 questions about bank callback post request
- Go language unit test 5: go language uses go sqlmock and Gorm to do database query mock
- [how to earn a million passive income]
- Conversion function and explicit
- Field problems in MySQL
猜你喜欢
Flutter动态化 | Fair 2.5.0 新版本特性
Unity EmbeddedBrowser浏览器插件事件通讯
Resolved (error in viewing data information in machine learning) attributeerror: target_ names
Go language unit test 4: go language uses gomonkey to test functions or methods
Brief analysis of tensorboard visual processing cases
[understanding by chance-37]: the structure of human sensory system determines that human beings are self-centered
太阳底下无新事,元宇宙能否更上层楼?
Logback log sorting
NFT new opportunity, multimedia NFT aggregation platform okaleido will be launched soon
The solution of Chinese font garbled code in keil5
随机推荐
Universal dividend source code, supports the dividend of any B on the BSC
Go 1.16.4: manage third-party libraries with Mod
Several common optimization methods matlab principle and depth analysis
Replace the GPU card number when pytorch loads the historical model, map_ Location settings
Ocean CMS vulnerability - search php
Internet of things completion -- (stm32f407 connects to cloud platform detection data)
AI 考高数得分 81,网友:AI 模型也免不了“内卷”!
NFT新的契机,多媒体NFT聚合平台OKALEIDO即将上线
3D视觉——2.人体姿态估计(Pose Estimation)入门——OpenPose含安装、编译、使用(单帧、实时视频)
常见的几种最优化方法Matlab原理和深度分析
实现CNN图像的识别和训练通过tensorflow框架对cifar10数据集等方法的处理
8 Queen question
Realize the recognition and training of CNN images, and process the cifar10 data set and other methods through the tensorflow framework
软件测试工作那么难找,只有外包offer,我该去么?
MySQL functions and related cases and exercises
[technology development-24]: characteristics of existing IOT communication technology
Logback log sorting
【556. 下一个更大元素 III】
Students who do not understand the code can also send their own token, which is easy to learn BSC
Go language web development series 30: gin: grouping by version for routing