pyqt4文档阅读(6.1):QBoxLayout-QHBoxLayout-QVBoxLayout

来源:互联网 发布:高收益网络理财平台 编辑:程序博客网 时间:2024/05/20 07:33

本系列文章长期更新修改.


QBoxLayout是常用的线性布局,如果说QGridLayout是二维上的布局,QBoxLayout就是一维上的布局.

QHBoxLayout和QVBoxLayout继承自QBoxLayout,分别是水平和垂直的盒子布局.


属性:


QBoxLayout

Types

  • enum Direction { LeftToRight, RightToLeft, TopToBottom, BottomToTop, Down, Up }

Methods

  • __init__ (self, Direction direction, QWidget parent = None)
  • addItem (self, QLayoutItem)
  • addLayout (self, QLayout layout, int stretch = 0)
  • addSpacerItem (self, QSpacerItem spacerItem)
  • addSpacing (self, int size)
  • addStretch (self, int stretch = 0)
  • addStrut (self, int)
  • addWidget (self, QWidget, int stretch = 0, Qt.Alignment alignment = 0)
  • int count (self)
  • Direction direction (self)
  • Qt.Orientations expandingDirections (self)
  • bool hasHeightForWidth (self)
  • int heightForWidth (self, int)
  • insertItem (self, int index, QLayoutItem)
  • insertLayout (self, int index, QLayout layout, int stretch = 0)
  • insertSpacerItem (self, int index, QSpacerItem spacerItem)
  • insertSpacing (self, int index, int size)
  • insertStretch (self, int index, int stretch = 0)
  • insertWidget (self, int index, QWidget widget, int stretch = 0, Qt.Alignment alignment = 0)
  • invalidate (self)
  • QLayoutItem itemAt (self, int)
  • QSize maximumSize (self)
  • int minimumHeightForWidth (self, int)
  • QSize minimumSize (self)
  • setDirection (self, Direction)
  • setGeometry (self, QRect)
  • setSpacing (self, int spacing)
  • setStretch (self, int index, int stretch)
  • bool setStretchFactor (self, QWidget w, int stretch)
  • bool setStretchFactor (self, QLayout l, int stretch)
  • QSize sizeHint (self)
  • int spacing (self)
  • int stretch (self, int index)
  • QLayoutItem takeAt (self, int)

QHBoxLayout & QVBoxLayout

Methods

  • __init__ (self)
  • __init__ (self, QWidget parent)


详细分析:


1.布局方向

我们说QBoxLayout是一维的布局类型,那么它需要知道这条线的方向.

比如垂直还是水平,从左到右还是从右到左.于是,一共可以得到4个方向,

  • enum Direction { LeftToRight, RightToLeft, TopToBottom, BottomToTop }

在初始化的时候我们就需要指定方向,当然也可以中途重新设置.

  • __init__ (self, Direction direction, QWidget parent = None)
  • Direction direction (self)
  • setDirection (self, Direction)

QHBoxLayout和QVBoxLayout实际上就是从左到右和从上到下的QBoxLayout.

从上面的函数列表也可以看到,它们并没有其他特殊的函数.

2.添加控件或布局

QBoxLayout和QGridLayout一样,添加控件和添加布局需要使用不同的函数.

另外,QBoxLayout还区分添加到末尾还是插入到第几个.

  • addItem (self, QLayoutItem)
  • addLayout (self, QLayout layout, int stretch = 0)
  • addWidget (self, QWidget, int stretch = 0, Qt.Alignment alignment = 0)
  • insertItem (self, int index, QLayoutItem)
  • insertLayout (self, int index, QLayout layout, int stretch = 0)
  • insertWidget (self, int index, QWidget widget, int stretch = 0, Qt.Alignment alignment = 0)

addItem和insertItem为虚函数,一般不使用.

参数里的stretch为控件/布局的比例因子,alignment是控件/布局的对齐方式(参考QGridLayout篇).

在添加控件/布局后,我们可以重新设置它们的比例因子.

  • bool setStretchFactor (self, QWidget w, int stretch)
  • bool setStretchFactor (self, QLayout l, int stretch)

函数的返回值表示这个控件/布局是否存在于这个布局中.

3.添加空白

QBoxLayout可以通过spacing和stretch来添加空白,spacing为定长的空白,stretch为定比例的空白(参考QGridLayout篇).

Qt里还有一个类QSpacerItem来表示空白区域,但实际上我们并不需要真正使用它们.

  • addSpacing (self, int size)
  • addStretch (self, int stretch = 0)
  • addSpacerItem (self, QSpacerItem spacerItem)
  • insertSpacing (self, int index, int size)
  • insertStretch (self, int index, int stretch = 0)
  • insertSpacerItem (self, int index, QSpacerItem spacerItem)

注意,这些空白的添加与QGridLayout里不同,它们会占据下标的位置,你大可以理解为,就是添加/插入了一个QSpacerItem.

添加后的stretch还能进行修改等操作:

  • int stretch (self, int index)
  • setStretch (self, int index, int stretch)

注意,这里下标对应的一定要是stretch.

4.控件间距

与QGridLayout一样,QBoxLayout也有统一的间距设置.

  • int spacing (self)
  • setSpacing (self, int spacing)

这个setSpacing的间距与上面的addSpacing或insertSpacing的不同点在于,上面的间距是会占用下标位置,而这个间距则表示下标相邻的两个物体的间距.

5.参考QGridLayout

尺寸获取

  • QSize maximumSize (self)
  • QSize minimumSize (self)

宽度高度纠缠

  • bool hasHeightForWidth (self)
  • int heightForWidth (self, int)
  • int minimumHeightForWidth (self, int)

数值获取

  • int count (self)

参考QGridLayout篇.

6.待续

addStrut (self, int)
Qt.Orientations expandingDirections (self)
invalidate (self)
QLayoutItem itemAt (self, int)
setGeometry (self, QRect)
QSize sizeHint (self)
QLayoutItem takeAt (self, int)


0 0
原创粉丝点击