Skip to content

QFormLayout


QFormLayout is a layout manager that arranges widgets in a two-column layout. The left column contains labels, and the right column contains widgets. This layout can also be achieved using QGridLayout but QFormLayout provides a more convenient way to create form-like layouts.


Methods


Examples


layout = QtWidgets.QFormLayout()
label = QtWidgets.QLabel("Name:")
line_edit = QtWidgets.QLineEdit()
layout.addRow(label, line_edit)

print(layout.rowCount())
1
spin_box = QtWidgets.QSpinBox()
layout.addRow("age", spinbox)

layout.removeRow(0)
print(layout.rowCount())
1



addRow(label, field)

Add a row to the form layout with a label and a field. The label can be a string or a widget. The field can be a widget or a layout.

Parameters:

Name Type Description Default
label QWidget | str

The label for the field.

required
field QLayout | QWidget

The field to add.

required


horizontalSpacing()

Get the horizontal spacing between the items in the layout.

Returns:

Type Description
int

The horizontal spacing between the items in the layout.


removeRow(row)

Remove the row from the layout. The row can be specified by the row number, the widget, or the layout.

Parameters:

Name Type Description Default
row int | QWidget | QLayout

The row to remove.

required


rowCount()

Get the number of rows in the layout.

Returns:

Type Description
int

The number of rows in the layout.


setHorizontalSpacing(spacing)

Set the horizontal spacing between the items in the layout.

Parameters:

Name Type Description Default
spacing int

The horizontal spacing between the items in the layout.

required


setVerticalSpacing(spacing)

Set the vertical spacing between the items in the layout.

Parameters:

Name Type Description Default
spacing int

The vertical spacing between the items in the layout.

required


verticalSpacing()

Get the vertical spacing between the items in the layout.

Returns:

Type Description
int

The vertical spacing between the items in the layout.