QLayout窗口布局

博客地址已更改,文章数量较多不便批量修改,若想访问源文请到 coologic博客 查阅,网址:www.coologic.cn

如本文记录地址为 techieliang.com/A/B/C/ 请改为 www.coologic.cn/A/B/C/ 即可查阅

版权声明:若无来源注明,Techie亮博客文章均为原创。 转载请以链接形式标明本文标题和地址:
本文标题: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行两列表单,提供了一套insertRowremoveRowaddRow的方法,此类默认第一列为QLabel,支持第一列只提供字符串而不提供QLabel对象

表单换行策略

setRowWrapPolicy(RowWrapPolicy policy)

ConstantValueDescription
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

ConstantValueDescription
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

适用于复杂布局

  1. void addLayout(QLayout *layout, int row, int column, Qt::Alignment alignment = Qt::Alignment())
  2. void addLayout(QLayout *layout, int row, int column, int rowSpan, int columnSpan, Qt::Alignment alignment = Qt::Alignment())
  3. void addWidget(QWidget *widget, int row, int column, Qt::Alignment alignment = Qt::Alignment())
  4. void addWidget(QWidget *widget, int fromRow, int fromColumn, int rowSpan, int columnSpan, Qt::Alignment alignment = Qt::Alignment())

注意row,column为起点位置,rowSpan,columnSpan表示占用的行数,类似于excel中的合并单元格效果

  1. void setColumnStretch(int column, int stretch)
  2. void setRowStretch(int row, int stretch)
  3. void setHorizontalSpacing(int spacing)
  4. 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) 添加窗口

注意是窗口分割 不是布局分割,所以不能支持布局的添加

转载请以链接形式标明本文标题和地址:Techie亮博客 » QLayout窗口布局
原文地址:https://www.cnblogs.com/techiel/p/8036983.html