博客地址已更改,文章數量較多不便批量修改,若想訪問源文請到 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行兩列表單,提供了一套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) 添加窗口
注意是窗口分割 不是布局分割,所以不能支持布局的添加