Learn Python PyQt

QButtonGroup example for PyQt

QButtonGroup is inherited from QObject and is not a visual class, it is mainly a grouping of keys for easy key management.

By default, QRadioButton is mutually exclusive with all radio boxes of the same parent class, which can be classified by QButtonGroup.

Book: Create Desktop Apps with Python PyQt5

QButtonGroup

The example below creates two button groups (QButtonGroup). It adds radio buttons to each group (QRadioButton).

from PyQt5.Qt import *
import sys    

app = QApplication(sys.argv)

w = QWidget()
w.setWindowTitle("QButtonGrounp")
w.resize(300, 150)

cs1 = QRadioButton("Beginner",w)
cs1.move(130, 20)

cs2 = QRadioButton("Senior",w)
cs2.move(130, 40)

cs3 = QRadioButton("Expert",w)
cs3.move(130, 60)

cs4 = QRadioButton("Best",w)
cs4.move(130, 80)

cs_group = QButtonGroup(w)
cs_group.addButton(cs1)
cs_group.addButton(cs2)
cs_group.addButton(cs3)
cs_group.addButton(cs4)

drs1 = QRadioButton("Python",w)
drs1.move(20, 20)

drs2 = QRadioButton("Golang",w)
drs2.move(20, 40)

drs3 = QRadioButton("Java",w)
drs3.move(20, 60)

drs_group = QButtonGroup(w)
drs_group.addButton(drs1)
drs_group.addButton(drs2)
drs_group.addButton(drs3)

w.show()

if __name__ == '__main__':
    sys.exit(app.exec_())
pyqt qbuttongroup example

It can be seen that the buttons are divided into 2 groups. Groups are mutually exclusive, and difference in one group does not affect the other.

QButtonGroup id

QButtonGroup can quickly set an ID for a button when adding a button (do not specify an ID that will automatically assign a negative value), or you can view all buttons via buttons() and buttons() to view the specified button.

from PyQt5.Qt import *
import sys

app = QApplication(sys.argv)

w = QWidget()
w.setWindowTitle("QButtonGrounp")
w.resize(300, 150)

cs1 = QRadioButton("Answer A",w)
cs1.move(20, 20)
cs1.setChecked(True)

cs2 = QRadioButton("Answer B",w)
cs2.move(20, 40)

cs_group = QButtonGroup(w)
cs_group.addButton(cs1, 1)      # ID 1
cs_group.addButton(cs2, 2)      # ID 2

print(cs_group.buttons())
print(cs_group.button(2))       # ID=2
print(cs_group.checkedButton()

w.show()

if __name__ == '__main__':
    sys.exit(app.exec_())
qbuttongroup pyqt setid python

QButtonGroup setid

By default, the radio box of the same button group is mutually exclusive, i.e. only one button can be selected at the same time.

In QRadioButton, you can set a single button not to be mutually exclusive with other buttons, QButtonGroup has a similar function, setExclusive().

python pyqt qbuttongroup

The following program illustrates the use of setExclusive().

from PyQt5.Qt import *
import sys

app = QApplication(sys.argv)

w = QWidget()
w.setWindowTitle("QButtonGroup")
w.resize (300, 150)

#Create group 1 QRadioButton button
cs1 = QRadioButton ("Super Cup", w)
cs1.move(80, 20)

cs2 = QRadioButton ("Big Cup", w)
cs2.move(80, 40)

CS3 = QRadioButton ("Medium Cup", w)
cs3.move(80, 60)

cs4 = QRadioButton ("small cup", w)
cs4.move(80, 80)

#Create a key group and add keys
cs_group = QButtonGroup(w)
cs_group.addButton(cs1)
cs_group.addButton(cs2)
cs_group.addButton(cs3)
cs_group.addButton(cs4)

# Set cs_group to not be mutually exclusive
cs_group.setExclusive(False)

w.show()

if __name__ == '__main__':
    sys.exit(app.exec_())

Remove button

The remove button here means to move the button out of the QButtonGroup, not to remove this control, which is done by removeButton().

from PyQt5.Qt import *
import sys

app = QApplication(sys.argv)

w = QWidget()
w.setWindowTitle("QButtonGroup")
w.resize (300, 150)

#Create group 1 QRadioButton button
cs1 = QRadioButton("1st",w)
cs1.move(20, 20)

#Setting the first radio box to be checked
cs1.setChecked(True)
cs2 = QRadioButton("2nd",w)
cs2.move(20, 40)

#Create a key group and add keys
cs_group = QButtonGroup(w)
cs_group.addButton(cs1, 1)
cs_group.addButton(cs2, 2)

#move cs2 key out of sc_group
cs_group.removeButton(cs2)

w.show()

if __name__ == '__main__':
    sys.exit(app.exec_())

Signals

In the case of buttonClicked, the designated signal is id.

from PyQt5.Qt import *

import sys

app = QApplication(sys.argv)

w = QWidget()
w.setWindowTitle("QButtonGrounp")
w.resize (300, 150)

# Create 4 QRadioButton buttons
cs1 = QRadioButton ("Super Cup", w)
cs1.move(80, 20)

cs2 = QRadioButton ("Big Cup", w)
cs2.move(80, 40)

cs3 = QRadioButton ("Medium Cup", w)
cs3.move(80, 60)

cs4 = QRadioButton ("small cup", w)
cs4.move(80, 80)

# Create a key group and add keys
cs_group = QButtonGroup(w)
cs_group.addButton(cs1,1)
cs_group.addButton(cs2,2)
cs_group.addButton(cs3,3)
cs_group.addButton(cs4,4)

# Define the slot function and print the received parameters
def slot(object):
    print("Key was pressed, id is:", cs_group.id(object))

# connects the slot function and makes the argument of the band int type
cs_group.buttonClicked.connect(slot)
w.show()

if __name__ == '__main__':
    sys.exit(app.exec_())
pyqt qbuttongroup id

Book: Create Desktop Apps with Python PyQt5