PyQt - QButtonGroup



A group is a collection of similar objects. This grouping is established through classes like QButtonGroup or by arranging them using Layout Management. The UI component is more user-friendly when the user uses this class to build the application. Using QButtonGroup ensures that buttons behave together because it deals with the set of related options. For example, in a radio button group, only one option can be selected.

The following key purposes of QButtonGroup −

  • It simplifies event handling.
  • It makes an exclusive choice when used to select any single option.
  • It helps the user to understand the relationship between different elements and makes the application more interactive.
  • The arrangement and positioning of widgets can be controlled through layout management.

Components of QButtonGroup

Building the radio button using QButtonGroup involves various components −

QRadioButton − This is the actual button that builds the checkboxes to be checked or unchecked.

Note that, the function QRadioButton can be called through addWidget() in the layout section.

QRadioButton("option_name")

QButtonGroup − The class itself performs a built-in method i.e. used to build the radio button together. It ensures that only one radio button within the group can be checked at any given time.

QButtonGroup

Layout − The layout is the optional measure to use classes like QVBoxLayout and QHBoxLayout that arrange and organize the radio buttons in a specific order. It is useful when we have multiple radio buttons.

QVBoxLayout()

Orientation of Radio Button

  • QHBoxLayout − This class is used to contruct horizontal layout for horizontal arrangement.
  • QVBoxLayout − This class is used to contruct vertical layout for vertical arrangement.
radio button orientation

Radio Button using QVBoxLayout

We can use PyQt library to create a simple GUI with radio buttons. The class QVBoxLayout creates a group of radio buttons for programming languages (C, C++, Java, Perl, Python) and when a radio button is toggled, the on_button_toggled method prints the selected language.

Example

Following example illustrate the code snippet of radio button using QVBoxLayout.

import sys
from PyQt6.QtWidgets import QApplication, QWidget, QVBoxLayout, QRadioButton, QButtonGroup

class MyWidget(QWidget):
   def __init__(self):
      super().__init__()

      layout = QVBoxLayout(self)
      button_group = QButtonGroup(self)
      # If the user wants to perform an action on the options
      button_group.buttonToggled.connect(self.on_button_toggled)
      for text in ['C', 'C++', 'Java', 'Perl', 'Python']:
         radio_button = QRadioButton(text)
         layout.addWidget(radio_button)
         button_group.addButton(radio_button)

   def on_button_toggled(self, button, checked):
      if checked:
         print(f'Button "{button.text()}" is checked.')

if __name__ == '__main__':
   app = QApplication(sys.argv)
   widget = MyWidget()
   widget.show()
   sys.exit(app.exec())

Output

The above code produces the following outcome −

QButtonGroup example one

Radio Button using QHBoxLayout

In PyQt, we create a window with choices (Linux, Windows, Mac OS) using radio buttons. It prints the chosen option when a button is clicked and displays the selected option when another button is pressed.

Example

Following example illustrates the code for users to select the option in the PyQt Window(horizontal orientation).

import sys
from PyQt6.QtWidgets import QApplication, QWidget, QHBoxLayout, QRadioButton, 
QButtonGroup, QPushButton

class ButtonGroupExample(QWidget):
   def __init__(self):
      super().__init__()
      self.initUI()

   def initUI(self):
      # Create a horizontal layout
      hbox = QHBoxLayout()

      # Create radio buttons
      radio_button1 = QRadioButton('Linux')
      radio_button2 = QRadioButton('Window')
      radio_button3 = QRadioButton('Mac Os')

      # Create a button group and add radio buttons to it
      button_group = QButtonGroup()
      button_group.addButton(radio_button1)
      button_group.addButton(radio_button2)
      button_group.addButton(radio_button3)

      # Set the default checked button
      radio_button1.setChecked(True)

      # Connect a slot to handle button clicks
      button_group.buttonClicked.connect(self.on_button_clicked)

      # Add radio buttons to the layout
      hbox.addWidget(radio_button1)
      hbox.addWidget(radio_button2)
      hbox.addWidget(radio_button3)

      # Add a button to show the selected option
      show_selected_button = QPushButton('Operating System')
      show_selected_button.clicked.connect(self.show_selected_option)
      hbox.addWidget(show_selected_button)

      # Set the layout for the main window
      self.setLayout(hbox)

      # Set window properties
      self.setGeometry(300, 300, 300, 200)
      self.setWindowTitle('Horizontal Radio Button')

   def on_button_clicked(self, button):
      print(f'Button clicked: {button.text()}')

   def show_selected_option(self):
      selected_button = self.sender().group().checkedButton()
      if selected_button:
         print(f'Selected option: {selected_button.text()}')
      else:
         print('No option selected')

if __name__ == '__main__':
   app = QApplication(sys.argv)
   window = ButtonGroupExample()
   window.show()
   sys.exit(app.exec())

Output

The above code produces the following Output −

QButtonGroup example two
Advertisements