Qt之格栅布局(QGridLayout)
栅格布局将位于其中的窗口部件放入一个网状的栅格之中。QGridLayout需要将提供给它的空间划分成的行和列,并把每个窗口部件插入并管理到正确的单元格。 栅格布局是这样工作的:
它计算了位于其中的空间,然后将它们合理的划分成若干个行(row)和列(column),并把每个由它管理的窗口部件放置在合适的单元之中,这里所指的单元(cell)即是指由行和列交叉所划分出来的空间。
在栅格布局中,行和列本质上是相同的,只是叫法不同而已。下面将重点讨论列,这些内容当然也适用于行。
在栅格布局中,每个列(以及行)都有一个最小宽度(使用setColumnMinimumWidth()设置)以及一个伸缩因子(使用setColumnStretch()设置)。最小宽度指的是位于该列中的窗口部件的最小的宽度,而伸缩因子决定了该列内的窗口部件能够获得多少空间。
一般情况下我们都是把某个窗口部件放进栅格布局的一个单元中,但窗口部件有时也可能会需要占用多个单元。这时就需要用到addWidget()方法的一个重载版本,原型如下:
void addWidget(QWidget *, int row, int column, int rowSpan, int columnSpan, Qt::Alignment = 0);
这个单元将从row和column开始,扩展到rowSpan和columnSpan指定的倍数的行和列。如果rowSpan或columnSpan的值为-1,则窗口部件将扩展到布局的底部或者右边边缘处。
在创建栅格布局完成后,就可以使用addWidget(),addItem(),以及addLayout()方法向其中加入窗口部件,以及其它的布局。
QLineEdit *pUserLineEdit = new QLineEdit(this); QLineEdit *pPasswordLineEdit = new QLineEdit(this); QCheckBox *pRememberCheckBox = new QCheckBox(this); QCheckBox *pAutoLoginCheckBox = new QCheckBox(this); QPushButton *pLoginButton = new QPushButton(this); QPushButton *pRegisterButton = new QPushButton(this); QPushButton *pForgotButton = new QPushButton(this); pLoginButton- setFixedHeight(30); pUserLineEdit- setFixedWidth(200); // 设置头像 QPixmap pixmap(":/Images/logo"); pImageLabel- setFixedSize(90, 90); pImageLabel- setPixmap(pixmap); pImageLabel- setScaledContents(true); // 设置文本 pUserLineEdit- setPlaceholderText(QStringLiteral("QQ号码/手机/邮箱")); pPasswordLineEdit- setPlaceholderText(QStringLiteral("密码")); pPasswordLineEdit- setEchoMode(QLineEdit::Password); pRememberCheckBox- setText(QStringLiteral("记住密码")); pAutoLoginCheckBox- setText(QStringLiteral("自动登录")); pLoginButton- setText(QStringLiteral("登录")); pRegisterButton- setText(QStringLiteral("注册账号")); pForgotButton- setText(QStringLiteral("找回密码")); QGridLayout *pLayout = new QGridLayout(); // 头像 第0行,第0列开始,占3行1列 pLayout- addWidget(pImageLabel, 0, 0, 3, 1); // 用户名输入框 第0行,第1列开始,占1行2列 pLayout- addWidget(pUserLineEdit, 0, 1, 1, 2); pLayout- addWidget(pRegisterButton, 0, 4); // 密码输入框 第1行,第1列开始,占1行2列 pLayout- addWidget(pPasswordLineEdit, 1, 1, 1, 2); pLayout- addWidget(pForgotButton, 1, 4); // 记住密码 第2行,第1列开始,占1行1列 水平居左 垂直居中 pLayout- addWidget(pRememberCheckBox, 2, 1, 1, 1, Qt::AlignLeft | Qt::AlignVCenter); // 自动登录 第2行,第2列开始,占1行1列 水平居右 垂直居中 pLayout- addWidget(pAutoLoginCheckBox, 2, 2, 1, 1, Qt::AlignRight | Qt::AlignVCenter); // 登录按钮 第3行,第1列开始,占1行2列 pLayout- addWidget(pLoginButton, 3, 1, 1, 2); // 设置水平间距 pLayout- setHorizontalSpacing(10); // 设置垂直间距 pLayout- setVerticalSpacing(10); // 设置外间距 pLayout- setContentsMargins(10, 10, 10, 10); setLayout(pLayout);
addWidget(QWidget *, int row, int column, Qt::Alignment = 0) addWidget(QWidget *, int row, int column, int rowSpan, int columnSpan, Qt::Alignment = 0)
添加窗口部件至布局。
这个单元将从row和column开始,扩展到rowSpan和columnSpan指定的倍数的行和列。如果rowSpan或columnSpan的值为-1,则窗口部件将扩展到布局的底部或者右边边缘处,Qt::Alignment为对齐方式。
-addLayout(QLayout *, int row, int column, Qt::Alignment = 0)
-addLayout(QLayout *, int row, int column, int rowSpan, int columnSpan, Qt::Alignment = 0)
和addWidget类似,这个是添加布局。
setSpacing()可以同时设置水平、垂直间距,设置之后,水平、垂直间距相同。
setHorizontalSpacing()、setVerticalSpacing()可以分别设置水平间距、垂直间距。
当界面元素较为复杂时,应毫不犹豫的尽量使用栅格布局,而不是使用水平和垂直布局的组合或者嵌套的形式,因为在多数情况下,后者往往会使“局势”更加复杂而难以控制。栅格布局赋予了界面设计器更大的自由度来排列组合界面元素,而仅仅带来了微小的复杂度开销。
当要设计的界面是一种类似于两列和若干行组成的形式时,使用表单布局要比栅格布局更为方便些。
QT TextEdit控件 全面详解 本文详细的介绍了TextEdit控件的各种操作,例如:获取内容、输入控件字符、保持在最后一行添加(自动滚屏)、定时关闭、添加数据换行、向鼠标位置插入一行字符、设置字体颜色属性等操作。 本系列QT全面详解文章目前共有十五篇,本系列文章较为详细的讲述了QT控件的基础操作和使用,也谢谢大家的关注、点赞、收藏。
相关文章
- Qt之等待提示框(QTimer)
- Qt-QMenu的自定义内容-在右击列表中增加一个EDIT框
- QT-找开工程后,最上方提示the code model could not parse an included file, which might lead to incorrect code completion and highlighting, for example.
- Qt编写自定义控件36-图片浏览器
- Qt布局交换两个控件位置
- paip.ollydbg 设置c++ qt API断点总结
- Qt坐标系统
- Qt QDataStream的使用
- Qt QML的元素布局的实现
- Qt创建一个自定义按钮
- QT配置opencv_qt调用opencv
- 2、QT基础——创建Qt项目
- Qt设置全局文字大小
- Qt多线程间信号槽传递非QObject类型对象的参数
- QT-多线程与界面之间交互总结
- Qt配置文件操作
- Qt动态增加和动态删除QWidget内控件(有布局和没有布局都有效)(qDeleteAll、delete、deleteLater、findChildren)
- QT 开源类库集合
- 基于QT实现简单的音乐播放器
- QT中文显示乱码解决方式
- Qt创建非模态对话框
- Qt Quick 布局演示
- Qt编写可视化大屏电子看板系统4-布局另存