QButtonGroup, QHBoxLayout, and QVBoxLayout
Understand how we use QButton, QHBoxLayout, and QVBoxLayout widgets in our GUI applications.
We'll cover the following...
We'll cover the following...
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:
from PyQt6.QtWidgets import QButtonGroup, QCheckBoxb_group = QButtonGroup() # Create instance of QButtonGroup# Create two checkboxescb_1 = QCheckBox("CB 1")cb_2 = QCheckBox("CB 2")# Add checkboxes into QButtonGroupb_group.addButton(cb_1)b_group.addButton(cb_2)# Connect all buttons in a group to one signalb_group.buttonClicked.connect(cbClicked)def cbClicked(cb):print(cb)
First of all, we start by importing QButtonGroup and QCheckBox. Then we create an instance of ...