QLayout窗口布局
本文标题:QLayout窗口布局 本文地址:http://techieliang.com/2017/12/690/
1. 介绍
QLayout
| Header: | #include <QLayout> |
| qmake: | QT += widgets |
| Inherits: | QObject and QLayoutItem |
| Inherited By: | QBoxLayout, QFormLayout, QGridLayout, and QStackedLayout |
涉及到的控件主要有:QSplitter窗口分割器、QSpacerItem间距控制(类似于弹簧效果)、QHBoxLayout(1行n列)和QVBoxLayout(n行1列)行列布局、QFormLayout表单布局(n行2列)、QGridLayout栅格布局(n行n列)
addWidget(QWidget *w)、removeWidget(QWidget *widget)? QWidget操作
setSpacing(int spacing) setHorizontalSpacing(int spacing) setVerticalSpacing(int spacing)设置间距
1.1. QSpacerItem
在使用Designer时,就是Spacers里面的行列spacer,弹簧样式的图标,此控件添加以后不会在界面显示,主要是占位使用。任何layout默认是先符合控件的sizePolicy的要求下进行控件大小、间距调整。
但如果想要实现类似于程序标题栏的效果,左侧图标、程序名,右侧最大化、最小化关闭按钮,中间就需要一个占位的空白控件,这时候需要使用QSpacerItem。
在Designer时,直接拖拽到需要占位的地方(注意,两个空间之间或者布局之间均可,但其所在空间必须是QLayout而不是QWidget)
代码使用:使用addSpacerItem(QSpacerItem *spacerItem)、insertSpacerItem(int index, QSpacerItem *spacerItem)、removeItem(QLayoutItem *item)
addSpacing(int size)这类方法是设置间距而不是插入spaceritem
spacerItem父类是QLayoutItem,直接removeQLayoutItem 即可删除,同理可以使用removeItem(QLayoutItem *item)、
1.2. QHBoxLayout、QVBoxLayout
其父类为QBoxLayout,可以配合QSpacerItem使用
1.3. QFormLayout
n行两列表单,提供了一套insertRow、removeRow、addRow的方法,此类默认第一列为QLabel,支持第一列只提供字符串而不提供QLabel对象
表单换行策略
setRowWrapPolicy(RowWrapPolicy policy)
| Constant | Value | Description |
|---|---|---|
QFormLayout::DontWrapRows |
0 |
一直在一行Fields are always laid out next to their label. This is the default policy for all styles except Qt Extended styles. |
QFormLayout::WrapLongRows |
1 |
自适应,如果空间不够则两行Labels are given enough horizontal space to fit the widest label, and the rest of the space is given to the fields. If the minimum size of a field pair is wider than the available space, the field is wrapped to the next line. This is the default policy for Qt Extended styles. |
QFormLayout::WrapAllRows |
2 |
一直两行Fields are always laid out below their label. |
setWidget(int row, ItemRole role, QWidget *widget)
不使用addrow一类的整行添加,也可以逐个添加,使用此函数需要设置ItemRole
| Constant | Value | Description |
|---|---|---|
QFormLayout::LabelRole |
0 |
标签列A label widget. |
QFormLayout::FieldRole |
1 |
输入框列A field widget. |
QFormLayout::SpanningRole |
2 |
单控件占用整行A widget that spans label and field columns. |
1.4. QGridLayout
适用于复杂布局
- void addLayout(QLayout *layout, int row, int column, Qt::Alignment alignment = Qt::Alignment())
- void addLayout(QLayout *layout, int row, int column, int rowSpan, int columnSpan, Qt::Alignment alignment = Qt::Alignment())
- void addWidget(QWidget *widget, int row, int column, Qt::Alignment alignment = Qt::Alignment())
- void addWidget(QWidget *widget, int fromRow, int fromColumn, int rowSpan, int columnSpan, Qt::Alignment alignment = Qt::Alignment())
注意row,column为起点位置,rowSpan,columnSpan表示占用的行数,类似于excel中的合并单元格效果
- void setColumnStretch(int column, int stretch)
- void setRowStretch(int row, int stretch)
- void setHorizontalSpacing(int spacing)
- void setVerticalSpacing(int spacing)
设置伸缩空间和间距
1.5. QStackedLayout
堆布局,这个布局实现widget分页显示,Qt提供了他的一个控件QStackedWidget
1.6. QSplitter
实现窗口分割效果,且可以动态调整比例
setOpaqueResize(bool opaque = true) 在调整比例时是否动态更新
setChildrenCollapsible(bool) 是否允许子窗口为0尺寸
addWidget(QWidget *widget)、insertWidget(int index, QWidget *widget) 添加窗口
注意是窗口分割 不是布局分割,所以不能支持布局的添加
QLayout窗口布局的更多相关文章
- 06--Qt窗口布局
Qt窗口布局 标签: qtlayout 2012-05-05 07:56 3076人阅读 评论(0) 收藏 举报 分类: Qt开发(33) 版权声明:本文为博主原创文章,未经博主允许不得转载. 布 ...
- VS2013中如何更改主题颜色(深色)和恢复默认的窗口布局
1.通常情况下,我们会根据个人爱好更改VS2013的主题颜色,一开始我喜欢白色,后来我偏爱深色. 依次选择:工具->选项->常规->主题->深色->确定,ok 2.我们在 ...
- 保持plsql窗口布局
在window菜单中有个 save layout 项,设置好窗口布局后,选一下此项就保存你当前的窗口布局了,下次启动就不用再设置了.
- IDEA如何重置窗口布局
如何重置窗口布局 我不知道怎么搞的,左边的,上边的,下边的,视图都没有了 , 重启了一下,然后重置为默认视图,就好了
- IDEA的窗口布局设置
修改idea的窗口布局 idea默认的窗口模式是如: 可以通过File->Appearance->Window Options->勾选 Widescreen tool window ...
- Visual Studio 重置窗口布局
Visual Studio 重置窗口布局
- Window窗口布局 --- DecorView浅析
开发中,通常都是在onCreate()中调用setContentView(R.layout.custom_layout)来实现想要的页面布局,我们知道,页面都是依附在窗口之上的,而DecorView即 ...
- JavaFX--第3天窗口布局
1.windows之间的交互 2.关闭程序 3.布局镶嵌 1.windows之间的交互 我们要实现“确定”.“取消”之类的功能:就像我们平时使用Word的时候要关闭会提示要不要保存的信息. 步骤如下: ...
- QT+动手设计一个登陆窗口+布局
登陆窗口的样式如下: 这里面涉及着窗口的UI设计,重点是局部布局和整体布局, 首先在ui窗口上添加一个容器类(Widget),然后将需要添加的控件放置在容器中,进行局部布局(在进行局部布局的时候可以使 ...
随机推荐
- 在docker中运行elasticsearch时go程序无法连接到节点
错误信息: panic: no active connection found: no Elasticsearch node available 在docker中运行es时,默认启动sniffing ...
- golang基础--Interface接口
接口是一个或多个方法签名名的集合,定义方式如下 type Interface_Name interface { method_a() string method_b() int .... } 只要某个 ...
- scala 求数组排序后每两个元素的差值
求数组排序后每两个元素的差值 例如数组 1,5,8,10,2 求得结果为 1,3,3,2 一般什么样的场景会有这种需求呢? 比如 计算一堆数据在一定时间内的计算时延, 或者得到这段时间内数据的平均计算 ...
- 准确率(accuracy),精确率(Precision),召回率(Recall)和综合评价指标(F1-Measure )----转
原文:http://blog.csdn.net/t710smgtwoshima/article/details/8215037 Recall(召回率);Precision(准确率);F1-Meat ...
- package html to native application
npm install nw -g npm . https://github.com/nwjs/nw.js/ https://github.com/nwjs/nw.js/wiki/How-to-run ...
- HTML页面参数的传递与获取
传递的页面: $("#btn").click(function () { window.location.href="diancan.html?a=1"; }) ...
- 20155233 2006-2007-2 《Java程序设计》第2周学习总结
20155233 2006-2007-2 <Java程序设计>第2周学习总结 教材学习内容总结 本章主要学习Java的基础语法,这些语法在C语言的学习中基本上都涉及到过,基本上,Java可 ...
- day 14 元组
1. 使用场景? # 列表list 数据类型相同, #rwx文件 100个人的名字, # 用字典 dict ['dɪkt] 很多信息描述1个人, # tuple [ˈtʌpəl] #只读文件 不能修改 ...
- IBM X3650M4简单排错方法
如果出问题了,首先看开机的那个地方的灯是否显示正常,有黄色的灯亮一般都不正常: 这种服务器带有一个错误指示板,只需要在电源开关那,如上图,把那个蓝色的按钮往里面按,就能把里面的一个板子抽出来,可以看到 ...
- 手摸手,和你一起学习 UiPath Studio
学习 RPA 的路上坑比较多,让我们手摸手,一起走…… 以下是一些学习 UiPath 和 RPA 的资源, 拿走不用谢! UiPath Studio 中文文档 机器人流程自动化其实是很好的概念和技术, ...