Qt布局管理使用

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)

Constant Value Description
QLayout::SetDefaultConstraint 0 The main widget's minimum size is set to minimumSize(), unless the widget already has a minimum size.
QLayout::SetFixedSize 3 The main widget's size is set to sizeHint(); it cannot be resized at all.
QLayout::SetMinimumSize 2 The main widget's minimum size is set to minimumSize(); it cannot be smaller.
QLayout::SetMaximumSize 4 The main widget's maximum size is set to maximumSize(); it cannot be larger.
QLayout::SetMinAndMaxSize 5 The main widget's minimum size is set to minimumSize() and its maximum size is set to maximumSize().
QLayout::SetNoConstraint 1 The widget is not constrained.

QSpacerItem的QSizePolicy:

Constant Value Description
QSizePolicy::Fixed 0 The 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::Minimum GrowFlag The 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::Maximum ShrinkFlag The 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::Preferred GrowFlag | ShrinkFlag The 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::Expanding GrowFlag | ShrinkFlag | ExpandFlag The 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::MinimumExpanding GrowFlag | ExpandFlag The 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::Ignored ShrinkFlag | GrowFlag | IgnoreFlag The 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都有充分的了解.



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值