QT6 源(41):QBoxLayout 及其俩子类 QHBoxLayout QVBoxLayout 的 源码。QBoxLayout : public QLayout

(1)qboxlayout .h 头文件

#ifndef QBOXLAYOUT_H
#define QBOXLAYOUT_H

#include <QtWidgets/qtwidgetsglobal.h>
#include <QtWidgets/qlayout.h>
#ifdef QT_INCLUDE_COMPAT
#include <QtWidgets/qwidget.h>
#endif

#include <limits.h>

QT_BEGIN_NAMESPACE


class QBoxLayoutPrivate;


//    class QWidget : public QObject, public QPaintDevice
//    class QLayout : public QObject, public QLayoutItem
//The QBoxLayout class lines up child widgets horizontally or vertically.
class Q_WIDGETS_EXPORT QBoxLayout : public QLayout
{
    Q_OBJECT  //又插入了这个宏,此宏会释放一些数据成员与函数成员
    Q_DECLARE_PRIVATE(QBoxLayout)
private:
    Q_DISABLE_COPY(QBoxLayout)   //禁止对本类对象的复制操作
public:
    enum Direction { LeftToRight, RightToLeft,
                     TopToBottom, BottomToTop,
                     Down = TopToBottom,
                     Up   = BottomToTop  }; //定义了枚举类

    explicit QBoxLayout(Direction, QWidget *parent = nullptr);
    //构造一个新的QBoxLayout,方向为dir,父容器为parent。
    //布局直接设置为 parent 的顶层布局。一个小部件只能有一个顶层布局。

    ~QBoxLayout();

    Direction direction() const;
    void   setDirection(Direction);

//Q_PROPERTY(int spacing READ spacing WRITE setSpacing) 布局内组件之间的间距
    int     spacing() const override;
    void setSpacing(int spacing) override;

    //Returns the stretch factor at position index.
    int     stretch(int index) const;
    void setStretch(int index, int stretch); //给 index控件设置伸缩因子
    bool setStretchFactor(QWidget *w, int stretch);
    //Sets the stretch factor for w to stretch and returns true if
    //widget is found in this layout (not including child layouts);
    //otherwise returns false.
    bool setStretchFactor(QLayout *l, int stretch);
    //Sets the stretch factor for the l layout to stretch and returns true if
    //layout is found in this layout (not including child layouts);
    //otherwise returns false.

    QSize sizeHint   () const override; //返回自己的最佳大小
    QSize minimumSize() const override; //返回自己的最小大小
    QSize maximumSize() const override; //返回自己的最大大小

    //返回此布局项的对于此宽度的首选高度,返 -1 表首选高度与项的宽度无关。
    //使用hasHeightForWidth()函数通常比调用此函数并测试-1要快得多。
    int        heightForWidth(int) const override;
    bool    hasHeightForWidth(   ) const override;
    //如果此布局的偏好高度取决于其宽度,则返回 true;否则返回 false。默认实现返回 false。
    int minimumHeightForWidth(int) const override;
    //返回此小部件在给定宽度下所需的最低高度。

    //返回值是 Qt::Vertical 或 Qt::Horizontal,布局内控件的增大伸展方向
    Qt::Orientations expandingDirections() const override;

    int count() const override;

    void invalidate() override; //Resets cached information.
    QLayoutItem *itemAt(int) const override;
    QLayoutItem *takeAt(int) override; //删除布局中的元素

    void setGeometry(const QRect& r) override; //set geometry to r.

    //为这个盒子布局的末尾添加一个不可拉伸的空间(一个QSpacerltem),其大小为size。
    //QBoxLayout提供了默认的边距和间距。此函数添加额外的空间。
    void addSpacing(int size);

    //为这个盒子布局的末尾添加一个可拉伸的空间(一个QSpacerltem),
    //其最小尺寸为0,拉伸因子为 stretch。
    void addStretch(int stretch = 0);

    //class QSpacerItem : public QLayoutItem
    //Adds spacerItem to the end of this box layout.
    void addSpacerItem(QSpacerItem *spacerItem);

    //将小部件添加到此框布局的末尾,拉伸因子为 stretch,对齐方式为 alignment。
    //拉伸因子仅适用于 QBoxLayout的方向,并且相对于此QBoxLayout 中的其他框和控件。
    //拉伸因子较高的控件和框会增长更多。
    //如果拉伸因子为0,并且QBoxLayout中的其他拉伸因子都不大于0,
    //则空间将根据每个参与Widget的QWidget:sizePolicy()进行分配。
    //对齐方式由alignment指定。默认对齐方式为 0,这意味着小部件将填充整个单元格。
    void addWidget(QWidget *, int stretch = 0,
                   Qt::Alignment alignment = Qt::Alignment());

    //Adds layout to the end of the box, with serial stretch factor stretch.
    void addLayout(QLayout *layout, int stretch = 0);

    //Limits the perpendicular垂直 dimension of the box
    //(e.g. height if the box is LeftToRight) to a minimum of size.
    //Other constraints may increase the limit.
    void addStrut(int);

    void addItem(QLayoutItem *) override;

    //Inserts a non-stretchable space (a QSpacerItem) at position index,
    //with size size. If index is negative the space is added at the end.
    //The box layout has default margin and spacing.
    //This function adds additional space.
    void insertSpacing(int index, int size);

    //Inserts a stretchable space (a QSpacerItem) at position index,
    //with zero minimum size and stretch factor stretch.
    //If index is negative the space is added at the end.
    void insertStretch(int index, int stretch = 0);

    //Inserts spacerItem at position index,
    //with zero minimum size and stretch factor.
    //If index is negative the space is added at the end.
    void insertSpacerItem(int index, QSpacerItem *spacerItem);

    //Inserts widget at position index, with stretch factor stretch and
    //alignment alignment. If index is negative, the widget is added at the end.
    void insertWidget(int index, QWidget *widget,
            int stretch = 0, Qt::Alignment alignment = Qt::Alignment());

    //Inserts layout at position index, with stretch factor stretch.
    //If index is negative, the layout is added at the end.
    //layout becomes a child of the box layout.
    void insertLayout(int index, QLayout *layout, int stretch = 0);

    //Inserts item into this box layout at position index.
    //If index is negative, the item is added at the end.
    void insertItem(int index, QLayoutItem *);
}; //完结 class QBoxLayout : public QLayout

class Q_WIDGETS_EXPORT QHBoxLayout : public QBoxLayout
{
    Q_OBJECT
private:
    Q_DISABLE_COPY(QHBoxLayout) //这俩构造函数,唯有参数不同
public: //Constructs a new top-level horizontal box with parent parent.
    QHBoxLayout();
    explicit QHBoxLayout(QWidget *parent);   
    ~QHBoxLayout();
};

class Q_WIDGETS_EXPORT QVBoxLayout : public QBoxLayout
{
    Q_OBJECT
private:
    Q_DISABLE_COPY(QVBoxLayout)
public: //Constructs a new top-level vertical box with parent parent.
    QVBoxLayout();
    explicit QVBoxLayout(QWidget *parent);
    ~QVBoxLayout();
};

QT_END_NAMESPACE

#endif // QBOXLAYOUT_H

(2)

在这里插入图片描述

(3)

谢谢

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值