当前位置:网站首页>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 支持 比例系数 的概念
- 比例系数 决定了组件大小的 相对变化
边栏推荐
- 【电脑插入U盘或者内存卡显示无法格式化FAT32如何解决】
- Richview trvstyle liststyle list style (bullet number)
- [技術發展-24]:現有物聯網通信技術特點
- 树的深入和广度优先遍历(不考虑二叉树)
- Kivy教程之 盒子布局 BoxLayout将子项排列在垂直或水平框中(教程含源码)
- Libuv库 - 设计概述(中文版)
- PHP maze game
- Brief analysis of tensorboard visual processing cases
- IBEM mathematical formula detection data set
- Go language web development series 26: Gin framework: demonstrates the execution sequence of code when there are multiple middleware
猜你喜欢
如何使用lxml判断网站公告是否更新
[redis] cache warm-up, cache avalanche and cache breakdown
使用vscode查看Hex或UTF-8编码
MySQL 数据增删改查综合案例
Go language web development series 29: Gin framework uses gin contrib / sessions library to manage sessions (based on cookies)
Internet of things completion -- (stm32f407 connects to cloud platform detection data)
[email protected] chianxin: Perspective of Russian Ukrainian cyber war - Security confrontation and sanctions g"/>
Start signing up CCF C ³- [email protected] chianxin: Perspective of Russian Ukrainian cyber war - Security confrontation and sanctions g
Go language web development series 28: solve cross domain access of CORS with gin contrib / CORS
SQL Injection (GET/Select)
双向链表(我们只需要关注插入和删除函数)
随机推荐
Go language web development series 26: Gin framework: demonstrates the execution sequence of code when there are multiple middleware
挡不住了,国产芯片再度突进,部分环节已进到4nm
树的深入和广度优先遍历(不考虑二叉树)
Red hat satellite 6: better management of servers and clouds
8 Queen question
Complete deep neural network CNN training with tensorflow to complete picture recognition case 2
Error running 'application' in idea running: the solution of command line is too long
Complete DNN deep neural network CNN training with tensorflow to complete image recognition cases
Golang — 命令行工具cobra
Halcon combined with C # to detect surface defects -- Halcon routine autobahn
Windos creates Cordova prompt because running scripts is prohibited on this system
Flutter dynamic | fair 2.5.0 new version features
logback日志的整理
Go language web development series 29: Gin framework uses gin contrib / sessions library to manage sessions (based on cookies)
Use and design of Muduo buffer class
Go: send the get request and parse the return JSON (go1.16.4)
MySQL 数据增删改查综合案例
Golang - command line tool Cobra
When updating mysql, the condition is a query
KEIL5出现中文字体乱码的解决方法