QGridLayout Class
The QGridLayout class lays out widgets in a grid of rows and columns. More…
| Header | #include <QGridLayout> |
|---|---|
| CMake | target_link_libraries(app PRIVATE Nt::Widgets) |
| Module | Nt Widgets C++ Classes |
| Inherits | QLayout |
List of all members, including inherited members
Public Functions
| QGridLayout(QWidget *parent = nullptr) | |
| ~QGridLayout() | |
| void | addWidget(QWidget *widget, int row, int column, Qt::Alignment alignment = Qt::Alignment()) |
| void | addWidget(QWidget *widget, int row, int column, int rowSpan, int columnSpan, 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 | addItem(QLayoutItem *item, int row, int column, int rowSpan = 1, int columnSpan = 1, Qt::Alignment alignment = Qt::Alignment()) |
| int | rowCount() const |
| int | columnCount() const |
| QLayoutItem * | itemAtPosition(int row, int column) const |
| void | getItemPosition(int index, int *row, int *column, int *rowSpan, int *columnSpan) const |
| void | setRowStretch(int row, int stretch) |
| int | rowStretch(int row) const |
| void | setColumnStretch(int column, int stretch) |
| int | columnStretch(int column) const |
| void | setRowMinimumHeight(int row, int height) |
| int | rowMinimumHeight(int row) const |
| void | setColumnMinimumWidth(int column, int width) |
| int | columnMinimumWidth(int column) const |
| int | horizontalSpacing() const |
| void | setHorizontalSpacing(int spacing) |
| int | verticalSpacing() const |
| void | setVerticalSpacing(int spacing) |
Reimplemented Public Functions
| void | addItem(QLayoutItem *item) |
| QLayoutItem * | itemAt(int index) const |
| QLayoutItem * | takeAt(int index) |
| QSize | sizeHint() const |
| QSize | minimumSize() const |
| Qt::Orientations | expandingDirections() const |
| void | setGeometry(const QRect &rect) |
Detailed Description
Each widget goes in a cell, or in a block of cells that it spans. A column is as wide as the widest widget in it needs, and a row as high as its highest widget; the space that is left over is shared out as in QBoxLayout: rows and columns with a stretch factor, or that hold widgets that want to expand, get it. Spacing is put between the rows and between the columns, and the margins around the grid.
QGridLayout *grid = new QGridLayout(window);
grid->addWidget(new QLabel("Name"), 0, 0);
grid->addWidget(new QLineEdit, 0, 1);
grid->addWidget(new QLabel("Notes"), 1, 0, Qt::AlignTop);
grid->addWidget(new QLineEdit, 1, 1);
grid->addWidget(okButton, 2, 0, 1, 2); // spans both columns
grid->setColumnStretch(1, 1);
A form in a grid: labels in the first column, fields that span two columns, and two buttons in the last row. The second and third columns have the same stretch factor.

See also QBoxLayout and QLayout.
Differences from Qt
There is no setOriginCorner(), cellRect(), setDefaultPositioning() and no addItem() overload that fills the cell by direction. Empty rows and columns have no spacing next to them. The height of a widget does not depend on its width (heightForWidth()), so a word-wrapped label needs a fixed height.
Member Function Documentation
QGridLayout::QGridLayout(QWidget *parent = nullptr)
Constructs a grid layout for parent, with no rows and columns. Rows and columns appear when widgets are added.
QGridLayout::~QGridLayout()
Destroys the layout. The widgets that are in it stay.
void QGridLayout::addItem(QLayoutItem *item, int row, int column, int rowSpan = 1, int columnSpan = 1, Qt::Alignment alignment = Qt::Alignment())
Adds item so that it covers rowSpan rows and columnSpan columns, from the cell at row and column, with the given alignment. The layout takes ownership of the item.
void QGridLayout::addItem(QLayoutItem *item)
Reimplements QLayout::addItem().
This is an overloaded function.
Adds item in the first free cell of the last row, or in a new row if that one is full.
void QGridLayout::addLayout(QLayout *layout, int row, int column, Qt::Alignment alignment = Qt::Alignment())
Adds layout in the cell at row and column, with the given alignment. It becomes a child of this layout.
See also addWidget().
void QGridLayout::addLayout(QLayout *layout, int row, int column, int rowSpan, int columnSpan, Qt::Alignment alignment = Qt::Alignment())
This is an overloaded function.
Adds layout so that it covers rowSpan rows and columnSpan columns, from the cell at row and column.
void QGridLayout::addWidget(QWidget *widget, int row, int column, Qt::Alignment alignment = Qt::Alignment())
Adds widget in the cell at row and column, with the given alignment: without it, the widget fills the cell as far as its size limits allow. The layout makes widget a child of the widget it manages.
See also addLayout() and addItem().
void QGridLayout::addWidget(QWidget *widget, int row, int column, int rowSpan, int columnSpan, Qt::Alignment alignment = Qt::Alignment())
This is an overloaded function.
Adds widget so that it covers rowSpan rows and columnSpan columns, from the cell at row and column.
int QGridLayout::columnCount() const
Returns the number of columns: one more than the last column that has an item.
int QGridLayout::columnMinimumWidth(int column) const
Returns the smallest width of column that was set, 0 by default.
See also setColumnMinimumWidth().
int QGridLayout::columnStretch(int column) const
Returns the stretch factor of column, 0 by default.
See also setColumnStretch().
Qt::Orientations QGridLayout::expandingDirections() const
Reimplements QLayout::expandingDirections().
void QGridLayout::getItemPosition(int index, int *row, int *column, int *rowSpan, int *columnSpan) const
Finds the position of the item at index and stores its row, column, rowSpan and columnSpan. Nothing is stored for an index that is not an item.
See also itemAtPosition().
int QGridLayout::horizontalSpacing() const
Returns the space between the columns: the one that setHorizontalSpacing() set, or else spacing().
See also setHorizontalSpacing() and verticalSpacing().
QLayoutItem * QGridLayout::itemAt(int index) const
Reimplements QLayout::itemAt().
QLayoutItem * QGridLayout::itemAtPosition(int row, int column) const
Returns the item that covers the cell at row and column, or nullptr if there is none.
See also getItemPosition().
QSize QGridLayout::minimumSize() const
Reimplements QLayout::minimumSize().
int QGridLayout::rowCount() const
Returns the number of rows: one more than the last row that has an item.
int QGridLayout::rowMinimumHeight(int row) const
Returns the smallest height of row that was set, 0 by default.
See also setRowMinimumHeight().
int QGridLayout::rowStretch(int row) const
Returns the stretch factor of row, 0 by default.
See also setRowStretch().
void QGridLayout::setColumnMinimumWidth(int column, int width)
Makes column at least width pixels wide, even if it is empty.
See also columnMinimumWidth().
void QGridLayout::setColumnStretch(int column, int stretch)
Sets the stretch factor of column to stretch.
See also columnStretch() and setRowStretch().
void QGridLayout::setGeometry(const QRect &rect)
Reimplements QLayout::setGeometry().
void QGridLayout::setHorizontalSpacing(int spacing)
Sets the space between the columns to spacing.
See also horizontalSpacing().
void QGridLayout::setRowMinimumHeight(int row, int height)
Makes row at least height pixels high, even if it is empty.
See also rowMinimumHeight().
void QGridLayout::setRowStretch(int row, int stretch)
Sets the stretch factor of row to stretch. Rows with a stretch factor share the space that is left over in proportion to the factors.
See also rowStretch() and setColumnStretch().
void QGridLayout::setVerticalSpacing(int spacing)
Sets the space between the rows to spacing.
See also verticalSpacing().
QSize QGridLayout::sizeHint() const
Reimplements QLayout::sizeHint().
QLayoutItem * QGridLayout::takeAt(int index)
Reimplements QLayout::takeAt().
int QGridLayout::verticalSpacing() const
Returns the space between the rows: the one that setVerticalSpacing() set, or else spacing().
See also setVerticalSpacing() and horizontalSpacing().