QT6 源(42):class QGridLayout : public QLayout : QObject + QLayoutItem。网格布局 GridLayout 的源码

(1)本源代码来自于头文件 qgridLayout . h

#ifndef QGRIDLAYOUT_H
#define QGRIDLAYOUT_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 QGridLayoutPrivate;


//    class QWidget : public QObject, public QPaintDevice
//    class QLayout : public QObject, public QLayoutItem
//The QGridLayout class lays out widgets in a grid.
class Q_WIDGETS_EXPORT QGridLayout : public QLayout
{
    Q_OBJECT  //又插入了此宏
    Q_DECLARE_PRIVATE(QGridLayout) //似乎是声明为私有的含义

    //这个属性持有并排排列的组件之间的间距如果未显式设置值,
    //则布局的横向间距将从父布局或父小部件的样式设置中继承。
    QDOC_PROPERTY(int horizontalSpacing
                 READ horizontalSpacing WRITE setHorizontalSpacing)

    QDOC_PROPERTY(int verticalSpacing
                 READ verticalSpacing   WRITE setVerticalSpacing)
    //此属性包含堆叠在一起的组件之间的间距。
    //如果没有显式设置值,则布局的垂直间距将继承自父布局或父小部件的样式设置。
protected:
    void addItem(QLayoutItem *) override;

private:
    Q_DISABLE_COPY(QGridLayout) //禁止复制

public:
    //Constructs a new QGridLayout with parent widget, parent.
    //The layout has one row and one column initially,
    //and will expand when new items are inserted.
    //If parent is nullptr, then you must insert this grid layout into
    //another layout, or set it as a widget's layout using QWidget::setLayout().
    explicit QGridLayout(QWidget *parent = nullptr);

    ~QGridLayout();

//    QDOC_PROPERTY(int horizontalSpacing
//                 READ horizontalSpacing WRITE setHorizontalSpacing)
    int                 horizontalSpacing() const;
    void             setHorizontalSpacing(int spacing);

//QDOC_PROPERTY(int verticalSpacing
//             READ verticalSpacing WRITE setVerticalSpacing)
    int             verticalSpacing() const;
    void         setVerticalSpacing(int spacing);

//Q_PROPERTY(int spacing READ spacing WRITE setSpacing) 来自父类QLayout
    int     spacing() const override;
    void setSpacing(int spacing) override;

    //Returns the stretch factor for row row.
    int     rowStretch(int row) const;
    void setRowStretch(int row, int stretch);
    //Sets the stretch factor of row row to stretch. The first row is number 0.
    //The stretch factor is relative to the other rows in this grid.
    //Rows with a higher stretch factor take more of the available space.
    //The default stretch factor is 0.
    //If the stretch factor is 0 and no other row in this table can grow at all,
    //the row may still grow.

    //Returns the minimum Height set for row row.
    int     rowMinimumHeight(int row) const;
    void setRowMinimumHeight(int row, int minSize);
    //Sets the minimum height of row row to minSize pixels.

    //Returns the stretch factor for column column.
    int     columnStretch(int column) const;
    void setColumnStretch(int column, int stretch);
    //Sets the stretch factor of column column to stretch.
    //The first column is number 0.

    //Returns the column spacing for column column.
    int     columnMinimumWidth(int column) const;
    void setColumnMinimumWidth(int column, int minSize);
    //Sets the minimum width of column column to minSize pixels.

/*  enum Corner {
        TopLeftCorner     = 0x00000,
        TopRightCorner    = 0x00001,
        BottomLeftCorner  = 0x00002,
        BottomRightCorner = 0x00003
    };
*/
//Returns the corner that's used for the grid's origin, i.e. for position (0, 0).
    Qt::Corner    originCorner() const;
    void       setOriginCorner(Qt::Corner);
    //Sets the grid's origin corner, i.e. position (0, 0), to corner.

    //return the number of items in the layout.
    int       count() const override;
    int    rowCount() const; //Returns the number of rows    in this grid.
    int columnCount() const; //Returns the number of columns in this grid.

    QSize sizeHint   () const override;
    QSize minimumSize() const override;
    QSize maximumSize() const override;

    int          heightForWidth(int) const override;
    bool      hasHeightForWidth(   ) const override;
    int   minimumHeightForWidth(int) const override;

    //返回网格中第 row 行和第 column列的单元格的几何形状。
    //如果行或列超出网格范围,则返回无效的矩形。
    //警告:在当前的 Qt版本中,此函数在调用setGeometry()之前
    //(即 parentWidget()可见之后)不会返回有效结果。
    QRect cellRect(int row, int column) const;
    //class  QRect { int x1;  int y1;  int x2;  int y2; }

    //returns the position information of the item with the given index.
    //The variables passed as row and column are updated with
    //the position of the item in the layout,
    //and the rowSpan and columnSpan variables are updated with the vertical and
    //horizontal spans of the item.
    void getItemPosition(int idx, int *row, int *column,
                         int *rowSpan, int *columnSpan) const;

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

    void setDefaultPositioning(int n, Qt::Orientation orient); //无注释

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

    void invalidate() override; //无效化此布局项中的任何缓存信息。

    QLayoutItem * itemAt(int index) const override;
    //Returns the layout item that occupies cell (row, column),
    //or nullptr if the cell is empty.
    QLayoutItem * itemAtPosition(int row, int column) const;
    QLayoutItem * takeAt(int index) override;


    //Adds item at position row, column, spanning rowSpan rows and
    //columnSpan columns, and aligns it according to alignment.
    //If rowSpan and/or columnSpan is -1, then the item will extend to the
    //bottom and/or right edge, respectively分别地.
    //The layout takes ownership of the item.
//Warning: Do not use this function to add child layouts or child widget items.
    //Use addLayout() or addWidget() instead.
    void addItem(QLayoutItem *item, int row, int column,
                 int rowSpan = 1, int columnSpan = 1,
                 Qt::Alignment = Qt::Alignment());

    //Adds widget w to this layout in a manner specific to the layout.
    inline void addWidget(QWidget *w) { QLayout::addWidget(w); }

    //Adds the given widget to the cell grid at row, column.
    //The top-left position is (0, 0) by default.
    //The alignment is specified by alignment. The default alignment is 0,
    //which means that the widget fills the entire cell.
    void addWidget(QWidget *, int row, int column,
                   Qt::Alignment = Qt::Alignment());

    void addWidget(QWidget *, int row, int column,
                   int rowSpan, int columnSpan, Qt::Alignment = Qt::Alignment());
    //This version adds the given widget to the cell grid,
    //spanning multiple rows/columns.
    //The cell will start at fromRow, fromColumn spanning rowSpan rows and
    //columnSpan columns. The widget will have the given alignment.
    //If rowSpan and/or columnSpan is -1,
    //then the widget will extend to the bottom and/or right edge, respectively.


    //Places the layout at position (row, column) in the grid.
    //The top-left position is (0, 0). The alignment is specified by alignment.
//The default alignment is 0, which means that the widget fills the entire cell.
    //A non-zero alignment indicates that the layout should not grow to
    //fill the available space but should be sized according to sizeHint().
    //layout becomes a child of the grid layout.
    void addLayout(QLayout *, int row, int column,
                   Qt::Alignment = Qt::Alignment());

    void addLayout(QLayout *, int row, int column,
                   int rowSpan, int columnSpan, Qt::Alignment = Qt::Alignment());
    //This version adds the layout layout to the cell grid,
    //spanning multiple rows/columns. The cell will start at row, column
    //spanning rowSpan rows and columnSpan columns.
    //If rowSpan and/or columnSpan is -1,
    //then the layout will extend to the bottom and/or right edge, respectively.
}; //完结class QGridLayout : public QLayout : public QObject, public QLayoutItem

QT_END_NAMESPACE

#endif // QGRIDLAYOUT_H

(2)

在这里插入图片描述

(3)

谢谢

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值