GridLayout:
Filter:
Classes | GUI > Layout

GridLayout : Layout : QObject : Object

A layout that organizes views in a grid
Source: QLayout.sc
Subclasses: QGridLayout

Description

GridLayout distributes its space into a grid of rows and columns, where each item can occupy one or more cells.

You can construct the layout in two ways using *rows and *columns. In the former constructor you pass arrays of items by rows, and in the latter by columns. Items can also be added later using -add and -addSpanning. To remove an item, simply use View: -remove for views, or QObject: -destroy for views or layouts.

It is possible to add more than one view into the same cell. The last added view will be the top-most. However, it is most probably more convenient to use a StackLayout for that purpose.

The layout manages the grid size automatically: you can add an item at any row and cell number. When items are added or removed, the grid will re-adjust according to the last occupied row and column.

Fine tuning

Each item can be assigned an alignment either at layout construction or later using -setAlignment. An item will then get at most its default size, if available (see: View: -sizeHint), and will be aligned within its cell according to the specified alignment.

Each row or column can be assigned a stretch factor using -setRowStretch and -setColumnStretch. Rows or columns that would otherwise get equal space are then distributed according to the relative proportions of their stretch factors.

Each row or column can also be assigned a minimum size using -setMinRowHeight and -setMinColumnWidth, to override the size constraints imposed by the contained views.

In addition to adjusting the spacing between cells using Layout: -spacing you can control the spacing between rows and between columns separately using -hSpacing and -vSpacing.

Leaving empty space

You can leave any cell empty by not placing any item into it, or at construction using nil instead of a view or another layout. Note though that the empty cells will always be regarded as freely stretchable and will not impose any constraints on space distribution.

Class Methods

GridLayout.rows( ... rows)

Creates a GridLayout and fills each row with an array of items given as arguments.

Arguments:

... rows

Each argument is an Array of items to form a consecutive row. An item can be a view, another layout, or nil for an empty cell.

Discussion:

You can make an item span more than one cell by wrapping it into an Array, followed by pairs of (\rows, number) and/or (\columns, number). You can also assign an alignment to an item by following it with a pair of (\align, alignment). \rows, \columns, and \align can be abbreviated with \r, \c, and \a, respectively. For possible alignment values see Alignment.

The simplified syntax for placing key-value pairs into an array comes handy (see Syntax Shortcuts: Creating Arrays with key-value pairs, and the example below).

Example:

GridLayout.columns( ... cols)

Creates a GridLayout and fills each column with an array of items given as arguments.

Arguments:

... cols

Each argument is an Array of items to form a consecutive column. An item can be a view, another layout, or nil for an empty cell.

Discussion:

To make an item span several cells, or assign an alignment to it, the same instructions as for *rows apply.

Inherited class methods

Undocumented class methods

GridLayout.new

GridLayout.parse(in, row, col)

Instance Methods

.add(item, row, column, align)

Adds an item into the cell at specified row and column.

Arguments:

item

The item can be a view or another layout.

row

The row index.

column

The column index.

align

A symbol denoting the alignment, or nil. See Alignment for possible values.

.addSpanning(item, row, column, rowSpan: 1, columnSpan: 1, align)

Adds an item into the grid so as to occupy several cells.

Arguments:

item

The item can be a view or another layout.

row

The row index.

column

The column index.

rowSpan

The amount of cells to occupy in vertical direction.

columnSpan

The amount of cells to occupy in horizontal direction.

align

A symbol denoting the alignment, or nil. See Alignment for possible values.

.hSpacing = spacing

The spacing between columns, in Integer amount of pixels.

.vSpacing = spacing

The spacing between rows, in Integer amount of pixels.

.setRowStretch(row, factor)

Sets the stretch factor of a row. By default rows have a stretch factor of 0. If a larger factor is assigned to a row, rows will get their space redistributed according to the relative proportions of their factors.

Arguments:

row

The index of a row.

factor

An Integer.

.setColumnStretch(column, factor)

Sets the stretch factor of a column. By default columns have a stretch factor of 0. If a larger factor is assigned to a column, columns will get their space redistributed according to the relative proportions of their factors.

Arguments:

column

The index of a column.

factor

An Integer.

.setAlignment(item, align)

Sets the alignment of an item managed by the layout.

Arguments:

item

A view or a layout managed by this layout, or a Point of which x denotes the column index and y the row index of an item.

align

A symbol denoting the alignment. See Alignment for possible values.

.minRowHeight(row)

Gets the minimum height assigned to a row.

Arguments:

row

The index of a row.

.setMinRowHeight(row, height)

Sets the minimum height of row.

Arguments:

row

The index of a row.

height

An Integer amount of pixels.

.minColumnWidth(column)

Gets the minimum width assigned to a column.

Arguments:

column

The index of a column.

.setMinColumnWidth(column, width)

Sets the minimum width of a column.

Arguments:

column

The index of a column.

width

An Integer amount of pixels.

Inherited instance methods