您的位置:首页 > 编程语言 > Qt开发

Qt布局管理使用

2013-04-27 21:48 519 查看
1.使用类

使用的类无非就是四个类QHBoxLayout, QVBoxLayout, QGridLayout, QFormLayout,

一般情况使用如下接口就够了:

addLayout(QLayout *layout, int stretch)

addWidget(QWidget *widget, int stretch)

addSpacing(int size),// 增加一个固定大小的项(占一个位置)

 addStretch(int stretch = 0),// 增加一个长度可增加的项(占一个位置) ,当增加这个以后,这行其它的widget,layout大小都会变为其sizeHint或者minimumsize,其它空间都被这个stretch占有(没有其它stretch)

(这两个都是addSpacerItem(QSpacerItem *spacerItem) 的简便函数.只是QSpacerItem的QSizePolicy不同而已)

setSpacing(int) // 各个控件间的空间

setContentsMargins(); // 边际到布局的空间

其它控件接口:

setSizeContraint(SizeContraint)

ConstantValueDescription
QLayout::SetDefaultConstraint0The main widget's minimum size is set to minimumSize(), unless the widget already has a minimum size.
QLayout::SetFixedSize3The main widget's size is set to sizeHint(); it cannot be resized at all.
QLayout::SetMinimumSize2The main widget's minimum size is set to minimumSize(); it cannot be smaller.
QLayout::SetMaximumSize4The main widget's maximum size is set to maximumSize(); it cannot be larger.
QLayout::SetMinAndMaxSize5The main widget's minimum size is set to minimumSize() and its maximum size is set to maximumSize().
QLayout::SetNoConstraint1The widget is not constrained.
QSpacerItem的QSizePolicy:

ConstantValueDescription
QSizePolicy::Fixed0The QWidget::sizeHint() is the only acceptable alternative, so the widget can never grow or shrink (e.g. the vertical direction of a push button).
QSizePolicy::MinimumGrowFlagThe sizeHint() is minimal, and sufficient. The widget can be expanded, but there is no advantage to it being larger (e.g. the horizontal direction of a push button). It cannot be smaller than the size provided by sizeHint().
QSizePolicy::MaximumShrinkFlagThe sizeHint() is a maximum. The widget can be shrunk any amount without detriment if other widgets need the space (e.g. a separator line). It cannot be larger than the size provided by sizeHint().
QSizePolicy::PreferredGrowFlag | ShrinkFlagThe sizeHint() is best, but the widget can be shrunk and still be useful. The widget can be expanded, but there is no advantage to it being larger than sizeHint() (the default QWidget policy).
QSizePolicy::ExpandingGrowFlag | ShrinkFlag | ExpandFlagThe sizeHint() is a sensible size, but the widget can be shrunk and still be useful. The widget can make use of extra space, so it should get as much space as possible (e.g. the horizontal direction of a horizontal slider).
QSizePolicy::MinimumExpandingGrowFlag | ExpandFlagThe sizeHint() is minimal, and sufficient. The widget can make use of extra space, so it should get as much space as possible (e.g. the horizontal direction of a horizontal slider).
QSizePolicy::IgnoredShrinkFlag | GrowFlag | IgnoreFlagThe sizeHint() is ignored. The widget will get as much space as possible.
2. 何时布局重新布局

1.QWidget发生一个QResizeEvent, 

2.QApplicationPrivate::notify_helper中,判断是否安装有布局,

3.如有则调用layout->widgetEvent(e);最终调用QLayout::setGeometry()将布局排好位置

(QApplicationPrivate::notify_helper->QLayout::widgetEvent->QLayoutPrivate::doResize->QBoxLayout::setGeometry)

3.快速开发中使用Ui Designer已经绰绰有余, 需要自定义窗口类我的使用四个布局类再加位置编码组合解决大部分问题, 至于自定义布局一般情况用不上.

3.布局实现过程

(1)布局中项由QBoxLayoutItem封装, 

(2)布局内部项使用QList<QBoxLayoutItem> list存储.

(3)在QLayout::setGeometry()中, 根据各个参数计算出项的QRect,设置位置

(4)要使用好布局类, 就要对控件的minimum, maxinmum, sizehint, sizepolicy都有充分的了解.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  Qt 布局