—— 均为自学记录,如有错误请指正
一、简述
QBoxLayout可以在水平方向或者垂直方向上排列控件,由QHBoxLayout、QVBoxLayout所继承。
- QHBoxLayout:水平布局
- QVBoxLayout:垂直布局
二、常用接口
例:
QHBoxlayout * pHlayout = new QHBoxLayout();
PHLayout->addStretch(); //伸缩空间
PHLayout->addWidget(控件名);
...
setLayout(PHLayout);
1. setMargin(int)
- 设置上下左右边距,均相同。
2. setContentsMargins(int left,int top,int right,int bottom)
- 设置边距,可不同。
3. setContentsMargins(const QMargins&margins)
- 设置边距,可不同。
4. setSpacing(int)
- 设置间距
5. addStretch()
- 添加一个伸缩空间。
- 在最后一个控件之后添加,所有控件居左;
- 在第一个控件前添加,所有控件居右;
- 在最后一个控件之后,第一个控件之前添加,所有控件居中;
- 在每个控件之间添加,每个控件之间间距相同“均分”。
6. addWidget(QWidget*,int stretch = 0,Qt::Alignment alignment = 0)
- 添加控件
- Qt::Alignment 对齐方式,默认居中
- Qt::Aligneft | Qt::AlignTop 水平居左,垂直居上(参数举例)
7. setDirection(Direction)
- 设置布局方向
- Direction可设置为:QBoxLayout::RightToLeft从左到右;QBoxLayout::TopToBottom从上到下
8. setStretchFactor(QWidget*w,int stretch) & setStretch(QLayout*l,int stretch);
- 设置控件,布局的拉伸系数