|  | Home · All Classes · Modules

# QGridLayout Class Reference
[QtGui module]

The QGridLayout class lays out widgets in a grid. More...

Inherits QLayout.

### Methods

- __init__ (self, QWidget)
- __init__ (self)
- addItem (self, QLayoutItem, int, int, int rowSpan = 1, int columnSpan = 1, Qt.Alignment alignment = 0)
- addItem (self, QLayoutItem)
- addLayout (self, QLayout, int, int, Qt.Alignment alignment = 0)
- addLayout (self, QLayout, int, int, int, int, Qt.Alignment alignment = 0)
- addWidget (self, QWidget)
- addWidget (self, QWidget, int, int, Qt.Alignment alignment = 0)
- addWidget (self, QWidget, int, int, int, int, Qt.Alignment alignment = 0)
- QRect cellRect (self, int, int)
- int columnCount (self)
- int columnMinimumWidth (self, int)
- int columnStretch (self, int)
- int count (self)
- Qt.Orientations expandingDirections (self)
- (int, int, int, int) getItemPosition (self, int)
- bool hasHeightForWidth (self)
- int heightForWidth (self, int)
- int horizontalSpacing (self)
- invalidate (self)
- QLayoutItem itemAt (self, int)
- QLayoutItem itemAtPosition (self, int, int)
- QSize maximumSize (self)
- int minimumHeightForWidth (self, int)
- QSize minimumSize (self)
- Qt.Corner originCorner (self)
- int rowCount (self)
- int rowMinimumHeight (self, int)
- int rowStretch (self, int)
- setColumnMinimumWidth (self, int, int)
- setColumnStretch (self, int, int)
- setDefaultPositioning (self, int, Qt.Orientation)
- setGeometry (self, QRect)
- setHorizontalSpacing (self, int)
- setOriginCorner (self, Qt.Corner)
- setRowMinimumHeight (self, int, int)
- setRowStretch (self, int, int)
- setSpacing (self, int)
- setVerticalSpacing (self, int)
- QSize sizeHint (self)
- int spacing (self)
- QLayoutItem takeAt (self, int)
- int verticalSpacing (self)

## Detailed Description

The QGridLayout class lays out widgets in a grid.

QGridLayout takes the space made available to it (by its parent layout or by the parentWidget()), divides it up into rows and columns, and puts each widget it manages into the correct cell.

Columns and rows behave identically; we will discuss columns, but there are equivalent functions for rows.

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() and addWidget(). If you do this, QGridLayout will guess how to distribute the size over the columns/rows (based on the stretch factors).

To remove a widget from a layout, call removeWidget(). Calling QWidget.hide() on a widget also effectively removes the widget from the layout until QWidget.show() is called.

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

Columns 0, 2 and 4 in this dialog fragment are made up of a QLabel, a 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() and setColumnStretch().

If the 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(), and addLayout().

QGridLayout also includes two margin widths: the contents margin and the 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.

See also QBoxLayout, QStackedLayout, Layout Management, and Basic Layouts Example.

## Method Documentation

### QGridLayout.__init__ (self, QWidget)

The QWidget argument, if not None, causes self to be owned by Qt instead of PyQt.

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.

### QGridLayout.__init__ (self)

Constructs a new grid layout.

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.addItem (self, QLayoutItem, int, int, int rowSpan = 1, int columnSpan = 1, Qt.Alignment alignment = 0)

The QLayoutItem argument has it's ownership transferred to Qt.

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.

### QGridLayout.addItem (self, QLayoutItem)

The QLayoutItem argument has it's ownership transferred to Qt.

Reimplemented from QLayout.addItem().

### QGridLayout.addLayout (self, QLayout, int, int, Qt.Alignment alignment = 0)

The QLayout argument has it's ownership transferred to Qt.

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.

### QGridLayout.addLayout (self, QLayout, int, int, int, int, Qt.Alignment alignment = 0)

The QLayout argument has it's ownership transferred to Qt.

This is an overloaded function.

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.

### QGridLayout.addWidget (self, QWidget)

The QWidget argument has it's ownership transferred to Qt.

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.

### QGridLayout.addWidget (self, QWidget, int, int, Qt.Alignment alignment = 0)

The QWidget argument has it's ownership transferred to Qt.

This is an overloaded function.

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.

### QGridLayout.addWidget (self, QWidget, int, int, int, int, Qt.Alignment alignment = 0)

The QWidget argument has it's ownership transferred to Qt.

### QRect QGridLayout.cellRect (self, int, int)

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.

Warning: 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 (self)

Returns the number of columns in this grid.

### int QGridLayout.columnMinimumWidth (self, int)

Returns the column spacing for column column.

See also setColumnMinimumWidth().

### int QGridLayout.columnStretch (self, int)

Returns the stretch factor for column column.

See also setColumnStretch().

### int QGridLayout.count (self)

Reimplemented from QLayout.count().

### Qt.Orientations QGridLayout.expandingDirections (self)

Reimplemented from QLayoutItem.expandingDirections().

### (int, int, int, int) QGridLayout.getItemPosition (self, int)

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.

See also itemAtPosition() and itemAt().

### bool QGridLayout.hasHeightForWidth (self)

Reimplemented from QLayoutItem.hasHeightForWidth().

### int QGridLayout.heightForWidth (self, int)

Reimplemented from QLayoutItem.heightForWidth().

### int QGridLayout.horizontalSpacing (self)

### QGridLayout.invalidate (self)

Reimplemented from QLayoutItem.invalidate().

### QLayoutItem QGridLayout.itemAt (self, int)

Reimplemented from QLayout.itemAt().

### QLayoutItem QGridLayout.itemAtPosition (self, int, int)

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

This function was introduced in Qt 4.4.

See also getItemPosition() and indexOf().

### QSize QGridLayout.maximumSize (self)

Reimplemented from QLayoutItem.maximumSize().

### int QGridLayout.minimumHeightForWidth (self, int)

Reimplemented from QLayoutItem.minimumHeightForWidth().

### QSize QGridLayout.minimumSize (self)

Reimplemented from QLayoutItem.minimumSize().

### Qt.Corner QGridLayout.originCorner (self)

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

See also setOriginCorner().

### int QGridLayout.rowCount (self)

Returns the number of rows in this grid.

### int QGridLayout.rowMinimumHeight (self, int)

Returns the minimum width set for row row.

See also setRowMinimumHeight().

### int QGridLayout.rowStretch (self, int)

Returns the stretch factor for row row.

See also setRowStretch().

### QGridLayout.setColumnMinimumWidth (self, int, int)

Sets the minimum width of column column to minSize pixels.

See also columnMinimumWidth() and setRowMinimumHeight().

### QGridLayout.setColumnStretch (self, int, int)

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.

See also columnStretch() and setRowStretch().

### QGridLayout.setDefaultPositioning (self, int, Qt.Orientation)

### QGridLayout.setGeometry (self, QRect)

Reimplemented from QLayoutItem.setGeometry().

### QGridLayout.setHorizontalSpacing (self, int)

### QGridLayout.setOriginCorner (self, Qt.Corner)

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

See also originCorner().

### QGridLayout.setRowMinimumHeight (self, int, int)

Sets the minimum height of row row to minSize pixels.

See also rowMinimumHeight() and setColumnMinimumWidth().

### QGridLayout.setRowStretch (self, int, int)

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.

See also rowStretch(), setRowMinimumHeight(), and setColumnStretch().

### QGridLayout.setSpacing (self, int)

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

See also spacing(), setVerticalSpacing(), and setHorizontalSpacing().

### QGridLayout.setVerticalSpacing (self, int)

### QSize QGridLayout.sizeHint (self)

Reimplemented from QLayoutItem.sizeHint().

### int QGridLayout.spacing (self)

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

See also setSpacing(), verticalSpacing(), and horizontalSpacing().

### QLayoutItem QGridLayout.takeAt (self, int)

The QLayoutItem result

Reimplemented from QLayout.takeAt().

### int QGridLayout.verticalSpacing (self)