Search⌘ K
AI Features

QButtonGroup, QHBoxLayout, and QVBoxLayout

Explore how to use QButtonGroup to group and manage buttons effectively within PyQt6. Understand the use of QHBoxLayout and QVBoxLayout to arrange widgets horizontally and vertically, and learn how to combine these layouts with addStretch() and setSpacing() for better GUI design. This lesson helps you organize your interface components cleanly and flexibly.

The QButtonGroup class

It's common to group some checkboxes or buttons to manage them more easily. Fortunately, PyQt offers the QButtonGroup class that can be used to group and organize buttons and make them mutually exclusive. This is also useful if you want one checkbox selected at a time.

QButtonGroup is a container to which widgets can be added rather than being a widget itself. As a result, adding QButtonGroup to a layout is not possible. The way for importing and setting up QButtonGroup in your program is demonstrated by the code below: ...

Python 3.8
from PyQt6.QtWidgets import QButtonGroup, QCheckBox
b_group = QButtonGroup() # Create instance of QButtonGroup
# Create two checkboxes
cb_1 = QCheckBox("CB 1")
cb_2 = QCheckBox("CB 2")
# Add checkboxes into QButtonGroup
b_group.addButton(cb_1)
b_group.addButton(cb_2)
# Connect all buttons in a group to one signal
b_group.buttonClicked.connect(cbClicked)
def cbClicked(cb):
print(cb)

First of all, ...