QGridLayout
The QGridLayout
class lays out widgets in a grid, which has a variable number of rows and columns. The grid layout is used
to create a more complex layout than the QHBoxLayout
and QVBoxLayout
classes,
which only allow for horizontal and vertical layouts, respectively.
Methods
addLayout
addWidget
columnCount
rowCount
setHorizontalSpacing
setVerticalSpacing
setColumnMinimumWidth
setRowMinimumHeight
setColumnStretch
setRowStretch
horizontalSpacing
verticalSpacing
columnMinimumWidth
rowMinimumHeight
columnStretch
rowStretch
Examples
layout = QtWidgets.QGridLayout()
layout.setHorizontalSpacing(10)
layout.setVerticalSpacing(10)
print(layout.horizontalSpacing(), layout.verticalSpacing())
10 10
layout.addWidget(QPushButton("Button 1"), 0, 0)
layout.addWidget(QPushButton("Button 2"), 0, 1)
layout.addWidget(QPushButton("Button 3"), 1, 0)
layout.addWidget(QPushButton("Button 4"), 1, 1)
print(layout.columnCount(), layout.rowCount())
2 2
addLayout(layout, row, column)
Add a layout to the grid layout at the specified row and column.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
layout |
QLayout
|
The layout to add. |
required |
row |
int
|
The row to add the layout to. |
required |
column |
int
|
The column to add the layout to. |
required |
addWidget(widget, row, column)
Add a widget to the grid layout at the specified row and column.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
widget |
QWidget
|
The widget to add. |
required |
row |
int
|
The row to add the widget to. |
required |
column |
int
|
The column to add the widget to. |
required |
columnCount()
Get the number of columns in the grid layout.
Returns:
Name | Type | Description |
---|---|---|
int |
int
|
The number of columns in the grid layout. |
columnMinimumWidth(column)
Get the minimum width of the specified column in the grid layout.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
column |
int
|
The column to get the minimum width of. |
required |
Returns:
Type | Description |
---|---|
int
|
The minimum width of the specified column in the grid layout. |
columnStretch(column)
Get the stretch factor of the specified column in the grid layout.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
column |
int
|
The column to get the stretch factor of. |
required |
Returns:
Name | Type | Description |
---|---|---|
int |
int
|
The stretch factor of the specified column in the grid layout. |
horizontalSpacing()
Get the horizontal spacing between items in the grid layout.
Returns:
Name | Type | Description |
---|---|---|
int |
int
|
The horizontal spacing between items in the grid layout. |
rowCount()
Get the number of rows in the grid layout.
Returns:
Name | Type | Description |
---|---|---|
int |
int
|
The number of rows in the grid layout. |
rowMinimumHeight(row)
Get the minimum height of the specified row in the grid layout.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
row |
int
|
The row to get the minimum height of. |
required |
Returns:
Name | Type | Description |
---|---|---|
int |
int
|
The minimum height of the specified row in the grid layout. |
rowStretch(row)
Get the stretch factor of the specified row in the grid layout.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
row |
int
|
The row to get the stretch factor of. |
required |
Returns:
Name | Type | Description |
---|---|---|
int |
int
|
The stretch factor of the specified row in the grid layout. |
setColumnMinimumWidth(column, minSize)
Set the minimum width of the specified column in the grid layout.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
column |
int
|
The column to set the minimum width of. |
required |
minSize |
int
|
The minimum width of the specified column. |
required |
setColumnStretch(column, stretch)
Set the stretch factor of the specified column in the grid layout.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
column |
int
|
The column to set the stretch factor of. |
required |
stretch |
int
|
The stretch factor of the specified column. |
required |
setHorizontalSpacing(spacing)
Set the horizontal spacing between items in the grid layout.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
spacing |
int
|
The horizontal spacing between items in the grid layout. |
required |
setRowMinimumHeight(row, minSize)
Set the minimum height of the specified row in the grid layout.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
row |
int
|
The row to set the minimum height of. |
required |
minSize |
int
|
The minimum height of the specified row. |
required |
setRowStretch(row, stretch)
Set the stretch factor of the specified row in the grid layout.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
row |
int
|
The row to set the stretch factor of. |
required |
stretch |
int
|
The stretch factor of the specified row. |
required |
setVerticalSpacing(spacing)
Set the vertical spacing between items in the grid layout.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
spacing |
int
|
The vertical spacing between items in the grid layout. |
required |
verticalSpacing()
Get the vertical spacing between items in the grid layout.
Returns:
Name | Type | Description |
---|---|---|
int |
int
|
The vertical spacing between items in the grid layout. |