QGridLayout 类

QGridLayout 类按栅格布局 Widget。 更多...

头: #include <QGridLayout>
CMake: find_package(Qt6 COMPONENTS Widgets REQUIRED)
target_link_libraries(mytarget PRIVATE Qt6::Widgets)
qmake: QT += widgets
继承: QLayout

特性

公共函数

  QGridLayout (QWidget * parent = nullptr)
virtual ~QGridLayout ()
void addItem (QLayoutItem * item , int row , int column , int rowSpan = 1, int columnSpan = 1, Qt::Alignment alignment = Qt::Alignment())
void addLayout (QLayout * layout , int row , int column , Qt::Alignment alignment = Qt::Alignment())
void addLayout (QLayout * layout , int row , int column , int rowSpan , int columnSpan , Qt::Alignment alignment = Qt::Alignment())
void addWidget (QWidget * widget , int row , int column , Qt::Alignment alignment = Qt::Alignment())
void addWidget (QWidget * widget , int fromRow , int fromColumn , int rowSpan , int columnSpan , Qt::Alignment alignment = Qt::Alignment())
QRect cellRect (int row , int column ) const
int columnCount () const
int columnMinimumWidth (int column ) const
int columnStretch (int column ) const
void getItemPosition (int index , int * row , int * column , int * rowSpan , int * columnSpan ) const
int horizontalSpacing () const
QLayoutItem * itemAtPosition (int row , int column ) const
Qt::Corner originCorner () const
int rowCount () const
int rowMinimumHeight (int row ) const
int rowStretch (int row ) const
void setColumnMinimumWidth (int column , int minSize )
void setColumnStretch (int column , int stretch )
void setHorizontalSpacing (int spacing )
void setOriginCorner (Qt::Corner corner )
void setRowMinimumHeight (int row , int minSize )
void setRowStretch (int row , int stretch )
void setVerticalSpacing (int spacing )
int verticalSpacing () const

重实现公共函数

virtual int count () const override
virtual Qt::Orientations expandingDirections () const override
virtual bool hasHeightForWidth () const override
virtual int heightForWidth (int w ) const override
virtual void invalidate () override
virtual QLayoutItem * itemAt (int index ) const override
virtual QSize maximumSize () const override
virtual int minimumHeightForWidth (int w ) const override
virtual QSize minimumSize () const override
virtual void setGeometry (const QRect & rect ) override
virtual void setSpacing (int spacing ) override
virtual QSize sizeHint () const override
virtual int spacing () const override
virtual QLayoutItem * takeAt (int index ) override

重实现保护函数

virtual void addItem (QLayoutItem * item ) override

详细描述

QGridLayout 占用可用于它的空间 (通过其父级布局或通过 parentWidget ()),将它划分成行和列,并将它管理的每个 Widget 放入正确单元格中。

列和行的行为等同;我们将讨论列,但行也有等效函数。

每列都有最小宽度和拉伸因子。最小宽度是最大设置宽度使用 setColumnMinimumWidth () 和该列中每个 Widget 的最小宽度。拉伸因子的设置是使用 setColumnStretch () 并确定列可用空间将超出其必要最小空间多少。

通常,会将被管理的每个 Widget 或布局放入其自己的单元格中使用 addWidget ()。Widget 占据多个单元格也是可能的,使用跨越行和列重载 addItem () 和 addWidget ()。若这样做,QGridLayout 将猜测如何覆盖列/行分配大小 (基于拉伸因子)。

要从布局移除 Widget,调用 removeWidget ()。调用 QWidget::hide () 在 Widget 还会从布局高效移除小部件,直到 QWidget::show () 被调用。

此插图展示具有 5 列 3 行的栅格的对话框片段 (以洋红色覆盖展示栅格):

A grid layout

组成此对话框片段中的第 0 列、第2 列和第 4 列的是 QLabel QLineEdit ,及 QListBox。第 1 列和第 3 列被做成占位符采用 setColumnMinimumWidth ()。第 0 行包含 3 个 QLabel 对象,第 1 行 3 个 QLineEdit 对象和第 2 行 3 个 QListBox 对象。使用占位符列 (1 和 3) 以获得在列之间的适量空间。

注意,列和行的宽和高并不相等。若想要 2 列拥有相同宽度,必须自己将它们的最小宽度和拉伸因子设为一样。这样做是使用 setColumnMinimumWidth () 和 setColumnStretch ().

若 QGridLayout 不是顶层布局 (即:不管理 Widget 的所有区域和子级),必须将它添加到其父级布局 (当创建它时),在采用它做任何事情之前。添加布局的平常方式是通过调用 addLayout () 在父级布局。

一旦有添加布局,就可以开始将 Widget 及其它布局放入栅格布局的单元格中使用 addWidget (), addItem (),和 addLayout ().

QGridLayout 还包括 2 种边距宽度: 内容边距 spacing ()。内容边距是沿 QGridLayout 4 侧边缘的预留空间宽度。 spacing () 是相邻框之间自动分配的间距宽度。

默认内容边距值的提供是通过 style 。Qt 样式指定的默认值对于 Widget 为 9,而对于窗口为 11。默认间距如同顶层布局 (或父级布局) 的边距宽度。

另请参阅 QBoxLayout , QStackedLayout , 布局管理 ,和 基本布局范例 .

特性文档编制

horizontalSpacing : int

This property holds the spacing between widgets that are laid out side by side

If no value is explicitly set, the layout's horizontal spacing is inherited from the parent layout, or from the style settings for the parent widget.

访问函数:

int horizontalSpacing () const
void setHorizontalSpacing (int spacing )

另请参阅 verticalSpacing , QStyle::pixelMetric (),和 PM_LayoutHorizontalSpacing .

verticalSpacing : int

This property holds the spacing between widgets that are laid out on top of each other

If no value is explicitly set, the layout's vertical spacing is inherited from the parent layout, or from the style settings for the parent widget.

访问函数:

int verticalSpacing () const
void setVerticalSpacing (int spacing )

另请参阅 horizontalSpacing , QStyle::pixelMetric (),和 PM_LayoutHorizontalSpacing .

成员函数文档编制

QGridLayout:: QGridLayout ( QWidget * parent = nullptr)

构造新的 QGridLayout 采用父级小部件 parent 。布局最初有一行和一列,且将展开当插入新的项时。

将直接把布局设为顶层布局对于 parent 。Widget 只可以有一个顶层布局。返回它通过 QWidget::layout ().

parent is nullptr ,那么必须将此栅格布局插入另一布局,或者将它设置为小部件的布局使用 QWidget::setLayout ().

另请参阅 QWidget::setLayout ().

[virtual] QGridLayout:: ~QGridLayout ()

销毁栅格布局。将终止几何体管理,若这是顶层栅格。

不销毁布局 Widget。

void QGridLayout:: addItem ( QLayoutItem * item , int row , int column , int rowSpan = 1, int columnSpan = 1, Qt::Alignment alignment = Qt::Alignment())

添加 item 在位置 row , column , spanning rowSpan rows and columnSpan columns, and aligns it according to alignment 。若 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 .

警告: 不要使用此函数添加子级布局 (或子级 Widget 项)。使用 addLayout () 或 addWidget () 代替。

[override virtual protected] void QGridLayout:: addItem ( QLayoutItem * item )

重实现: QLayout::addItem (QLayoutItem *item).

void QGridLayout:: addLayout ( QLayout * layout , int row , int column , Qt::Alignment alignment = Qt::Alignment())

放置 layout 在位置 ( row , column ) 在栅格中。左上位置为 (0, 0)。

对齐方式的指定通过 alignment 。默认对齐为 0,意味着 Widget 填充整个单元格。

A non-zero alignment indicates that the layout should not grow to fill the available space but should be sized according to sizeHint ().

layout 变为栅格布局的子级。

void QGridLayout:: addLayout ( QLayout * layout , int row , int column , int rowSpan , int columnSpan , Qt::Alignment alignment = Qt::Alignment())

这是重载函数。

此版本添加布局 layout to the cell grid, spanning multiple rows/columns. The cell will start at row , column spanning rowSpan rows and columnSpan columns.

rowSpan and/or columnSpan 为 -1,那么布局将分别延伸到底部和/或右边缘。

void QGridLayout:: addWidget ( QWidget * widget , int row , int column , Qt::Alignment alignment = Qt::Alignment())

添加给定 widget 到单元格栅格按 row , column 。左上位置默认为 (0, 0)。

对齐方式的指定通过 alignment 。默认对齐为 0,意味着 Widget 填充整个单元格。

void QGridLayout:: addWidget ( QWidget * widget , int fromRow , int fromColumn , int rowSpan , int columnSpan , Qt::Alignment alignment = Qt::Alignment())

这是重载函数。

此版本添加给定 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 .

rowSpan and/or columnSpan 为 -1,那么 Widget 将分别延伸到底部和/或右边缘。

QRect QGridLayout:: cellRect ( int row , int column ) const

Returns the geometry of the cell with row row and column column in the grid. Returns an invalid rectangle if row or column is outside the grid.

警告: in the current version of Qt this function does not return valid results until setGeometry () has been called, i.e. after the parentWidget () is visible.

int QGridLayout:: columnCount () const

返回此栅格的列数。

int QGridLayout:: columnMinimumWidth ( int column ) const

返回列间距为列 column .

另请参阅 setColumnMinimumWidth ().

int QGridLayout:: columnStretch ( int column ) const

返回拉伸因子为列 column .

另请参阅 setColumnStretch ().

[override virtual] int QGridLayout:: count () const

重实现: QLayout::count () const.

[override virtual] Qt::Orientations QGridLayout:: expandingDirections () const

重实现: QLayout::expandingDirections () const.

void QGridLayout:: getItemPosition ( int index , int * row , int * column , int * rowSpan , int * columnSpan ) const

返回项的位置信息采用给定 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.

另请参阅 itemAtPosition () 和 itemAt ().

[override virtual] bool QGridLayout:: hasHeightForWidth () const

重实现: QLayoutItem::hasHeightForWidth () const.

[override virtual] int QGridLayout:: heightForWidth ( int w ) const

重实现: QLayoutItem::heightForWidth (int) const.

[override virtual] void QGridLayout:: invalidate ()

重实现: QLayout::invalidate ().

[override virtual] QLayoutItem *QGridLayout:: itemAt ( int index ) const

重实现: QLayout::itemAt (int index) const.

QLayoutItem *QGridLayout:: itemAtPosition ( int row , int column ) const

返回的布局项占据单元格 ( row , column ),或 nullptr 若单元格为空。

另请参阅 getItemPosition () 和 indexOf ().

[override virtual] QSize QGridLayout:: maximumSize () const

重实现: QLayout::maximumSize () const.

[override virtual] int QGridLayout:: minimumHeightForWidth ( int w ) const

重实现: QLayoutItem::minimumHeightForWidth (int w) const.

[override virtual] QSize QGridLayout:: minimumSize () const

重实现: QLayout::minimumSize () const.

Qt::Corner QGridLayout:: originCorner () const

Returns the corner that's used for the grid's origin, i.e. for position (0, 0).

另请参阅 setOriginCorner ().

int QGridLayout:: rowCount () const

返回此网格中的行数。

int QGridLayout:: rowMinimumHeight ( int row ) const

返回设置的最小宽度为行 row .

另请参阅 setRowMinimumHeight ().

int QGridLayout:: rowStretch ( int row ) const

返回拉伸因子为行 row .

另请参阅 setRowStretch ().

void QGridLayout:: setColumnMinimumWidth ( int column , int minSize )

设置最小宽度为列 column to minSize 像素。

另请参阅 columnMinimumWidth () 和 setRowMinimumHeight ().

void QGridLayout:: setColumnStretch ( int column , int stretch )

设置拉伸因子为列 column to stretch 。第一列编号 0。

The stretch factor is relative to the other columns in this grid. Columns 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 column in this table can grow at all, the column may still grow.

An alternative approach is to add spacing using addItem () 采用 QSpacerItem .

另请参阅 columnStretch () 和 setRowStretch ().

[override virtual] void QGridLayout:: setGeometry (const QRect & rect )

重实现: QLayout::setGeometry (const QRect &r).

void QGridLayout:: setOriginCorner ( Qt::Corner corner )

Sets the grid's origin corner, i.e. position (0, 0), to corner .

另请参阅 originCorner ().

void QGridLayout:: setRowMinimumHeight ( int row , int minSize )

设置最小高度为行 row to minSize 像素。

另请参阅 rowMinimumHeight () 和 setColumnMinimumWidth ().

void QGridLayout:: setRowStretch ( int row , int stretch )

Sets the stretch factor of row row to stretch 。首行编号 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.

另请参阅 rowStretch (), setRowMinimumHeight (),和 setColumnStretch ().

[override virtual] void QGridLayout:: setSpacing ( int spacing )

重实现访问函数为特性: QLayout::spacing .

This function sets both the vertical and horizontal spacing to spacing .

另请参阅 spacing (), setVerticalSpacing (),和 setHorizontalSpacing ().

[override virtual] QSize QGridLayout:: sizeHint () const

重实现: QLayoutItem::sizeHint () const.

[override virtual] int QGridLayout:: spacing () const

重实现访问函数为特性: QLayout::spacing .

若垂直间距 == 水平间距,此函数返回该值;否则返回 -1。

另请参阅 setSpacing (), verticalSpacing (),和 horizontalSpacing ().

[override virtual] QLayoutItem *QGridLayout:: takeAt ( int index )

重实现: QLayout::takeAt (int index).