QLayouthtml
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列)app
addWidget(QWidget *w)、removeWidget(QWidget *widget)? QWidget操做ide
setSpacing(int spacing) setHorizontalSpacing(int spacing) setVerticalSpacing(int spacing)設置間距函數
在使用Designer時,就是Spacers裏面的行列spacer,彈簧樣式的圖標,此控件添加之後不會在界面顯示,主要是佔位使用。任何layout默認是先符合控件的sizePolicy的要求下進行控件大小、間距調整。佈局
但若是想要實現相似於程序標題欄的效果,左側圖標、程序名,右側最大化、最小化關閉按鈕,中間就須要一個佔位的空白控件,這時候須要使用QSpacerItem。post
在Designer時,直接拖拽到須要佔位的地方(注意,兩個空間之間或者佈局之間都可,但其所在空間必須是QLayout而不是QWidget)spa
代碼使用:使用addSpacerItem(QSpacerItem *spacerItem)、insertSpacerItem(int index, QSpacerItem *spacerItem)、removeItem(QLayoutItem *item)rest
addSpacing(int size)這類方法是設置間距而不是插入spaceritemexcel
spacerItem父類是QLayoutItem,直接removeQLayoutItem 便可刪除,同理能夠使用removeItem(QLayoutItem *item)、code
其父類爲QBoxLayout,能夠配合QSpacerItem使用
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. |
不使用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. |
適用於複雜佈局
注意row,column爲起點位置,rowSpan,columnSpan表示佔用的行數,相似於excel中的合併單元格效果
設置伸縮空間和間距
堆佈局,這個佈局實現widget分頁顯示,Qt提供了他的一個控件QStackedWidget
實現窗口分割效果,且能夠動態調整比例
setOpaqueResize(bool opaque = true) 在調整比例時是否動態更新
setChildrenCollapsible(bool) 是否容許子窗口爲0尺寸
addWidget(QWidget *widget)、insertWidget(int index, QWidget *widget) 添加窗口
注意是窗口分割 不是佈局分割,因此不能支持佈局的添加