1. 布局管理器中的比例系数
(1)默认情况下以等比例的方式更新组件大小
(2)可以自定义组件大小更新时的比例系数
2. QBoxLayout中的比例系数设置
(1)void setStretch(int index, int stretch); //设置指定索引对象的比例系数
(2)bool setStretchFactor(QWidget* widget, int stretch);
(3)bool setStretchFactor(QLayout* layout, int stretch);
▲组件的初始大小是独立于布局管理器设置的,因此不能保证组件的大小始终符合比例系统的设置。即当组件初次被显示时,其初始大小会以自己的方式显示。比例系数只有在其后大小发生变化时,才会起作用。
【编程实验】比例系数初探
//main.cpp
#include <QApplication> #include "Widget.h" int main(int argc, char *argv[]) { QApplication a(argc, argv); Widget w; w.show(); return a.exec(); }
//Widget.h
#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 initControl(); //通过绝对定位 void testVBoxLayout(); //垂直布局 void testHBoxLayout(); //水平布局 void testVHBoxLayout(); //嵌套布局 public: Widget(QWidget* parent = 0); ~Widget(); }; #endif //_WIDGET_H_
//Widget.cpp
#include "Widget.h" #include <QBoxLayout> #include <QDebug> Widget::Widget(QWidget* parent):QWidget(parent), TestBtn1(this),TestBtn2(this),TestBtn3(this),TestBtn4(this) { TestBtn1.setText("Test Button 1"); TestBtn2.setText("Test Button 2"); TestBtn3.setText("Test Button 3"); TestBtn4.setText("Test Button 4"); //绝对定位 //initControl(); //存在问题:位置和大小不能随父窗口的变化而变化 //垂直布局 testVBoxLayout(); //水平布局 //testHBoxLayout(); //嵌套布局 //testVHBoxLayout(); } void Widget::initControl() { TestBtn1.move(20, 20); TestBtn1.resize(160, 30); TestBtn2.move(20, 70); TestBtn2.resize(160, 30); TestBtn3.move(20, 120); TestBtn3.resize(160, 30); TestBtn4.move(20, 170); TestBtn4.resize(160, 30); } void Widget::testVBoxLayout() { QVBoxLayout* layout = new QVBoxLayout(); //水平和垂直方向扩展 TestBtn1.setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Expanding); TestBtn1.setMinimumSize(160, 30); TestBtn2.setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Expanding); TestBtn2.setMinimumSize(160, 30); TestBtn3.setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Expanding); TestBtn3.setMinimumSize(160, 30); TestBtn4.setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Expanding); TestBtn4.setMinimumSize(160, 30); layout->setSpacing(10); //间距10 layout->addWidget(&TestBtn1); layout->addWidget(&TestBtn2); layout->addWidget(&TestBtn3); layout->addWidget(&TestBtn4); //通过索引设置比例系数 layout->setStretch(0, 1); layout->setStretch(1, 1); layout->setStretch(2, 2); layout->setStretch(3, 2); setLayout(layout);//设置到本Widget对象,让布局对象生效 } void Widget::testHBoxLayout() { QHBoxLayout* layout = new QHBoxLayout(); //水平和垂直方向扩展 TestBtn1.setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Expanding); TestBtn1.setMinimumSize(160, 30); TestBtn2.setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Expanding); TestBtn2.setMinimumSize(160, 30); TestBtn3.setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Expanding); TestBtn3.setMinimumSize(160, 30); TestBtn4.setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Expanding); TestBtn4.setMinimumSize(160, 30); layout->setSpacing(10); //间距10 layout->addWidget(&TestBtn1); layout->addWidget(&TestBtn2); layout->addWidget(&TestBtn3); layout->addWidget(&TestBtn4); //通过组件设置比例系数 layout->setStretchFactor(&TestBtn1, 1); layout->setStretchFactor(&TestBtn2, 2); layout->setStretchFactor(&TestBtn3, 1); layout->setStretchFactor(&TestBtn4, 3); setLayout(layout);//设置到本Widget对象,让布局对象生效 } void Widget::testVHBoxLayout() { QHBoxLayout* hLayout1 = new QHBoxLayout(); QHBoxLayout* hLayout2 = new QHBoxLayout(); QVBoxLayout* vLayout = new QVBoxLayout(); //水平和垂直方向扩展 TestBtn1.setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Expanding); TestBtn1.setMinimumSize(160, 30); TestBtn2.setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Expanding); TestBtn2.setMinimumSize(160, 30); hLayout1->setSpacing(10); hLayout1->addWidget(&TestBtn1); hLayout1->addWidget(&TestBtn2); TestBtn3.setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Expanding); TestBtn3.setMinimumSize(160, 30); TestBtn4.setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Expanding); TestBtn4.setMinimumSize(160, 30); hLayout2->setSpacing(10); hLayout2->addWidget(&TestBtn3); hLayout2->addWidget(&TestBtn4); vLayout->setSpacing(10); //间距10 vLayout->addLayout(hLayout1); vLayout->addLayout(hLayout2); //1:3 vLayout->setStretchFactor(hLayout1, 1); vLayout->setStretchFactor(hLayout2, 3); setLayout(vLayout);//设置到本Widget对象,让布局对象生效 vLayout->update(); } Widget::~Widget() { }
3. QGridLayout布局管理器
(1)以网格(二维)的方式管理界面组件
(2)QGridLayout中的比例系数设置
①void setColumnStretch(int column, int stretch);//设置指定列的比例系数
②void setRowStretch(int row, int stretch); //设置指定行的比例系数
(3)QGridLayout支持嵌套其它布局管理器成为其管理对象
【编程实验】QGridLayout分析
//main.cpp
#include <QApplication> #include "Widget.h" int main(int argc, char *argv[]) { QApplication a(argc, argv); Widget w; w.show(); return a.exec(); }
//Widget.h
#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 testGridLayout1(); //一般的网格布局 void testGridLayout2(); //跨越网格的布局 public: Widget(QWidget* parent = 0); ~Widget(); }; #endif //_WIDGET_H_
//Widget.cpp
#include "Widget.h" #include <QGridLayout> #include <QDebug> Widget::Widget(QWidget* parent):QWidget(parent), TestBtn1(this),TestBtn2(this),TestBtn3(this),TestBtn4(this) { TestBtn1.setText("Test Button 1"); TestBtn2.setText("Test Button 2"); TestBtn3.setText("Test Button 3"); TestBtn4.setText("Test Button 4"); //一般的网格布局 //testGridLayout1(); //跨越网格的布局(不建议这样用) testGridLayout2(); } void Widget::testGridLayout1() { QGridLayout* layout = new QGridLayout(); //水平和垂直方向扩展 TestBtn1.setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Expanding); TestBtn1.setMinimumSize(160, 30); TestBtn2.setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Expanding); TestBtn2.setMinimumSize(160, 30); TestBtn3.setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Expanding); TestBtn3.setMinimumSize(160, 30); TestBtn4.setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Expanding); TestBtn4.setMinimumSize(160, 30); layout->setSpacing(10); //间距10 layout->addWidget(&TestBtn1, 0, 0); //第0行0列 layout->addWidget(&TestBtn2, 0, 1); //第0行1列 layout->addWidget(&TestBtn3, 1, 0); //第1行0列 layout->addWidget(&TestBtn4, 1, 1); //第1行1列 //设置比例系数 layout->setRowStretch(0, 1); //第0行,比例系数为1 layout->setRowStretch(1, 3); //第1行,比例系数为1 layout->setColumnStretch(0, 1); //第0列,比例系数为1 layout->setColumnStretch(1, 3); //第1列,比例系数为3 setLayout(layout);//设置到本Widget对象,让布局对象生效 } void Widget::testGridLayout2() { QGridLayout* layout = new QGridLayout(); //水平和垂直方向扩展 TestBtn1.setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Expanding); TestBtn1.setMinimumSize(160, 30); TestBtn2.setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Expanding); TestBtn2.setMinimumSize(160, 30); TestBtn3.setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Expanding); TestBtn3.setMinimumSize(160, 30); TestBtn4.setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Expanding); TestBtn4.setMinimumSize(160, 30); layout->setSpacing(10); //间距10 //addWidget(QWidget *widget, int fromRow, int fromColumn, // int rowSpan, int columnSpan, // Qt::Alignment alignment = Qt::Alignment()) layout->addWidget(&TestBtn1, 0, 0, 2, 1); //从(0,0)开始,占2行1列 layout->addWidget(&TestBtn2, 0, 1, 2, 1); //从(0,1)开始,占2行1列 layout->addWidget(&TestBtn3, 2, 0, 1, 2); //从(2,0)开始,占1行2列 layout->addWidget(&TestBtn4, 3, 0, 1, 2); //从(3,0)开始,占1行2列 setLayout(layout);//设置到本Widget对象,让布局对象生效 } Widget::~Widget() { }
4. 小结
(1)QGridLayout以网格的方式对组件进行管理
(2)QGridLayout中的组件可以根据需要跨越多个网格
(3)QBoxLayout和QGridLayout支持比例系数的概念
(4)比例系数决定了组件大小的相对变化