QGridLayout 类

QGridLayout class lays out widgets in a grid. 更多...

头: #include <QGridLayout>
qmake: QT += widgets
继承: QLayout

特性

公共函数

QGridLayout (QWidget * parent )
QGridLayout ()
~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 setSpacing (int spacing )
void setVerticalSpacing (int spacing )
int spacing () const
int verticalSpacing () const

重实现公共函数

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

重实现保护函数

virtual void addItem (QLayoutItem * item )

额外继承成员

详细描述

QGridLayout class lays out widgets in a grid.

QGridLayout takes the space made available to it (by its parent layout or by the parentWidget ()),将它划分成行和列,并将它管理的每个 Widget 放入正确单元格中。

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

Each column has a minimum width and a stretch factor. The minimum width is the greatest of that set using setColumnMinimumWidth () and the minimum width of each widget in that column. The stretch factor is set using setColumnStretch () and determines how much of the available space the column will get over and above its necessary minimum.

Normally, each managed widget or layout is put into a cell of its own using addWidget (). It is also possible for a widget to occupy multiple cells using the row and column spanning overloads of addItem () 和 addWidget (). If you do this, QGridLayout will guess how to distribute the size over the columns/rows (based on the stretch factors).

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

This illustration shows a fragment of a dialog with a five-column, three-row grid (the grid is shown overlaid in magenta):

A grid layout

Columns 0, 2 and 4 in this dialog fragment are made up of a QLabel QLineEdit , and a QListBox. Columns 1 and 3 are placeholders made with setColumnMinimumWidth (). Row 0 consists of three QLabel objects, row 1 of three QLineEdit objects and row 2 of three QListBox objects. We used placeholder columns (1 and 3) to get the right amount of space between the columns.

Note that the columns and rows are not equally wide or tall. If you want two columns to have the same width, you must set their minimum widths and stretch factors to be the same yourself. You do this using setColumnMinimumWidth () 和 setColumnStretch ().

QGridLayout is not the top-level layout (i.e. does not manage all of the widget's area and children), you must add it to its parent layout when you create it, but before you do anything with it. The normal way to add a layout is by calling addLayout () on the parent layout.

Once you have added your layout you can start putting widgets and other layouts into the cells of your grid layout using addWidget (), addItem (),和 addLayout ().

QGridLayout also includes two margin widths: the contents margin spacing (). The contents margin is the width of the reserved space along each of the QGridLayout 's four sides. The spacing () is the width of the automatically allocated spacing between neighboring boxes.

The default contents margin values are provided by the style . The default value Qt styles specify is 9 for child widgets and 11 for windows. The spacing defaults to the same as the margin width for a top-level layout, or to the same as the parent layout.

另请参阅 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.

该特性在 Qt 4.3 引入。

访问函数:

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.

该特性在 Qt 4.3 引入。

访问函数:

int verticalSpacing () const
void setVerticalSpacing (int spacing )

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

成员函数文档编制

QGridLayout:: QGridLayout ( QWidget * parent )

构造新的 QGridLayout with parent widget, parent . The layout has one row and one column initially, and will expand when new items are inserted.

QGridLayout:: QGridLayout ()

构造新栅格布局。

You must insert this grid into another layout. You can insert widgets and layouts into this layout at any time, but laying out will not be performed before this is inserted into another layout.

QGridLayout:: ~QGridLayout ()

Destroys the grid layout. Geometry management is terminated if this is a top-level grid.

布局的 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 .

警告: Do not use this function to add child layouts or child widget items. Use addLayout () 或 addWidget () 代替。

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

重实现自 QLayout::addItem ().

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

Places the layout 在位置 ( row , column ) in the grid. The top-left position is (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 becomes a child of the grid layout.

void QGridLayout:: addLayout ( QLayout * layout , int row , int column , int rowSpan , int columnSpan , Qt::Alignment 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.

rowSpan and/or columnSpan is -1, then the layout will extend to the bottom and/or right edge, respectively.

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

添加给定 widget to the cell grid at row , column . The top-left position is (0, 0) by default.

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

void QGridLayout:: addWidget ( QWidget * widget , int fromRow , int fromColumn , int rowSpan , int columnSpan , Qt::Alignment 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 .

rowSpan and/or columnSpan is -1, then the widget will extend to the bottom and/or right edge, respectively.

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

Returns the number of columns in this grid.

int QGridLayout:: columnMinimumWidth ( int column ) const

Returns the column spacing for column column .

另请参阅 setColumnMinimumWidth ().

int QGridLayout:: columnStretch ( int column ) const

Returns the stretch factor for column column .

另请参阅 setColumnStretch ().

[virtual] int QGridLayout:: count () const

重实现自 QLayout::count ().

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

重实现自 QLayoutItem::expandingDirections ().

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

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.

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

[virtual] bool QGridLayout:: hasHeightForWidth () const

重实现自 QLayoutItem::hasHeightForWidth ().

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

重实现自 QLayoutItem::heightForWidth ().

[virtual] void QGridLayout:: invalidate ()

重实现自 QLayoutItem::invalidate ().

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

重实现自 QLayout::itemAt ().

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

Returns the layout item that occupies cell ( row , column ), or 0 if the cell is empty.

该函数在 Qt 4.4 引入。

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

[virtual] QSize QGridLayout:: maximumSize () const

重实现自 QLayoutItem::maximumSize ().

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

重实现自 QLayoutItem::minimumHeightForWidth ().

[virtual] QSize QGridLayout:: minimumSize () const

重实现自 QLayoutItem::minimumSize ().

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

Returns the number of rows in this grid.

int QGridLayout:: rowMinimumHeight ( int row ) const

Returns the minimum width set for row row .

另请参阅 setRowMinimumHeight ().

int QGridLayout:: rowStretch ( int row ) const

Returns the stretch factor for row row .

另请参阅 setRowStretch ().

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

Sets the minimum width of column column to minSize 像素。

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

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

Sets the stretch factor of column column to stretch . The first column is number 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 () with a QSpacerItem .

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

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

重实现自 QLayoutItem::setGeometry ().

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 )

Sets the minimum height of row row to minSize 像素。

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

void QGridLayout:: 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.

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

void QGridLayout:: setSpacing ( int spacing )

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

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

[virtual] QSize QGridLayout:: sizeHint () const

重实现自 QLayoutItem::sizeHint ().

int QGridLayout:: spacing () const

If the vertical spacing is equal to the horizontal spacing, this function returns that value; otherwise it return -1.

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

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

重实现自 QLayout::takeAt ().