QFormLayout, QSpinBox and QComboBox

Understand how we use QFormLayout, QSpinBox, and QComboBox widgets in our GUI applications.

QFormLayout

PyQt offers the QFormLayout class for cases where you need to design a form to gather data from a user. It is a layout class that arranges child widgets into a two-column layout, with labels on the left and entry field widgets like QLineEdit or QSpinBox on the right. These GUIs are relatively easy to design, thanks to the QFormLayout class.

QSpinBox and QComboBox

Sometimes you may want the user to be limited to selecting options from a list of established values rather than utilizing a QLineEdit widget to input data. The devices QSpinBox and QComboBox are excellent tools for completing this activity.

When using QSpinBox, you can create a text-box-like object that lets users choose integer values by typing them in or using the up and down arrows. Additionally, you may modify the values' range, select the arrow's step size, specify a starting value, and even enter prefixes and suffixes inside the box. In PyQt, there are additional spin boxes for selecting date and time values, such as QDateEdit.

A list of choices can be displayed using QComboBox for the user to choose from. A pop-up list with various options is displayed when a user presses the arrow button. The creation of both types of objects, their addition to our layout, and how to use the values in QSpinBox to change other widgets in the GUI window are all covered in the code below.

We start by importing the required modules for this code.

Get hands-on with 1200+ tech skills courses.