PySide6 QRadioButton state reset

PySide6 QRadioButton state reset

QRadioButton state reset through QCheckBox unchecking.

Principle introduction

QRadioButton components are mutually exclusive through the QBottonGroup abstract button container. Then add custom properties to the QCheckBox button to get the object name of the QButtonGroup. Find the QRadioButton added to it through the QButtonGroup object name, and then reset the QRadioButton.

  1. Create the following GUI interface:
    GUI QRadioButton
    The GUI interface code is as follows:
from PySide6.QtCore import (QCoreApplication, QDate, QDateTime, QLocale,
    QMetaObject, QObject, QPoint, QRect,
    QSize, QTime, QUrl, Qt)
from PySide6.QtGui import (QBrush, QColor, QConicalGradient, QCursor,
    QFont, QFontDatabase, QGradient, QIcon,
    QImage, QKeySequence, QLinearGradient, QPainter,
    QPalette, QPixmap, QRadialGradient, QTransform)
from PySide6.QtWidgets import (QApplication, QCheckBox, QGroupBox, QMainWindow,
    QMenuBar, QPushButton, QRadioButton, QSizePolicy,
    QStatusBar, QWidget)

class Ui_testRadioButton(object):
    def setupUi(self, testRadioButton):
        if not testRadioButton.objectName():
            testRadioButton.setObjectName(u"testRadioButton")
        testRadioButton.setWindowModality(Qt.WindowModal)
        testRadioButton.resize(655, 284)
        font = QFont()
        font.setPointSize(10)
        testRadioButton.setFont(font)
        testRadioButton.setStyleSheet(u"groupbox {\\
"
"border: 0px;\\
"
"}")
        self.centralwidget = QWidget(testRadioButton)
        self.centralwidget.setObjectName(u"centralwidget")
        self.pushButton_ClosedWindow = QPushButton(self.centralwidget)
        self.pushButton_ClosedWindow.setObjectName(u"pushButton_ClosedWindow")
        self.pushButton_ClosedWindow.setGeometry(QRect(560, 210, 75, 24))
        self.pushButton_ClosedWindow.setMaximumSize(QSize(80, 16777215))
        self.pushButton_ClosedWindow.setFont(font)
        self.groupBox_QRadioButton = QGroupBox(self.centralwidget)
        self.groupBox_QRadioButton.setObjectName(u"groupBox_QRadioButton")
        self.groupBox_QRadioButton.setGeometry(QRect(40, 40, 491, 131))
        self.groupBox_QRadioButton.setFont(font)
        self.groupBox_QRadioButton.setStyleSheet(u"QGroupBox {\\
"
"background-color: transparent;\\
"
"border: 1px solid silver;\\
"
"border-radius: 6px;\\
"
"margin-top: 6px;\\
"
"}\\
"
"\\
"
"QGroupBox::title {\\
"
"subcontrol-origin: margin;\\
"
"left: 12px;\\
"
"padding: 0px 5px 0px 5px;\\
"
"}")
        self.groupBox_QRadioButton.setAlignment(Qt.AlignLeading|Qt.AlignLeft|Qt.AlignVCenter)
        self.groupBox_QRadioButton.setFlat(False)
        self.checkBox_Option1 = QCheckBox(self.groupBox_QRadioButton)
        self.checkBox_Option1.setObjectName(u"checkBox_Option1")
        self.checkBox_Option1.setGeometry(QRect(20, 30, 171, 20))
        self.radioButton_Right_Option1 = QRadioButton(self.groupBox_QRadioButton)
        self.radioButton_Right_Option1.setObjectName(u"radioButton_Right_Option1")
        self.radioButton_Right_Option1.setGeometry(QRect(280, 60, 171, 20))
        self.radioButton_Left_Option1 = QRadioButton(self.groupBox_QRadioButton)
        self.radioButton_Left_Option1.setObjectName(u"radioButton_Left_Option1")
        self.radioButton_Left_Option1.setGeometry(QRect(40, 60, 161, 20))
        self.checkBox_Option2 = QCheckBox(self.groupBox_QRadioButton)
        self.checkBox_Option2.setObjectName(u"checkBox_Option2")
        self.checkBox_Option2.setGeometry(QRect(260, 30, 191, 20))
        self.radioButton_Left_Option2 = QRadioButton(self.groupBox_QRadioButton)
        self.radioButton_Left_Option2.setObjectName(u"radioButton_Left_Option2")
        self.radioButton_Left_Option2.setGeometry(QRect(40, 90, 161, 20))
        self.radioButton_Right_Option2 = QRadioButton(self.groupBox_QRadioButton)
        self.radioButton_Right_Option2.setObjectName(u"radioButton_Right_Option2")
        self.radioButton_Right_Option2.setGeometry(QRect(280, 90, 171, 20))
        self.pushButton_QRadioOK = QPushButton(self.centralwidget)
        self.pushButton_QRadioOK.setObjectName(u"pushButton_QRadioOK")
        self.pushButton_QRadioOK.setGeometry(QRect(560, 150, 75, 24))
        testRadioButton.setCentralWidget(self.centralwidget)
        self.pushButton_ClosedWindow.raise_()
        self.pushButton_QRadioOK.raise_()
        self.groupBox_QRadioButton.raise_()
        self.menubar = QMenuBar(testRadioButton)
        self.menubar.setObjectName(u"menubar")
        self.menubar.setGeometry(QRect(0, 0, 655, 22))
        testRadioButton.setMenuBar(self.menubar)
        self.statusbar = QStatusBar(testRadioButton)
        self.statusbar.setObjectName(u"statusbar")
        testRadioButton.setStatusBar(self.statusbar)

        self.retranslateUi(testRadioButton)

        QMetaObject.connectSlotsByName(testRadioButton)
    # setupUi

    def retranslateUi(self, testRadioButton):
        testRadioButton.setWindowTitle(QCoreApplication.translate("testRadioButton", u"testQRadioButton", None))
        self.pushButton_ClosedWindow.setText(QCoreApplication.translate("testRadioButton", u"close", None))
        self.groupBox_QRadioButton.setTitle(QCoreApplication.translate("testRadioButton", u"QRadioButton", None))
        self.checkBox_Option1.setText(QCoreApplication.translate("testRadioButton", u"CheckBox_Option1", None))
        self.radioButton_Right_Option1.setText(QCoreApplication.translate("testRadioButton", u"radioButton_Right_Option1", None))
        self.radioButton_Left_Option1.setText(QCoreApplication.translate("testRadioButton", u"radioButton_Left_Option1", None))
        self.checkBox_Option2.setText(QCoreApplication.translate("testRadioButton", u"CheckBox_Option2", None))
        self.radioButton_Left_Option2.setText(QCoreApplication.translate("testRadioButton", u"radioButton_Left_Option2", None))
        self.radioButton_Right_Option2.setText(QCoreApplication.translate("testRadioButton", u"radioButton_Right_Option2", None))
        self.pushButton_QRadioOK.setText(QCoreApplication.translate("testRadioButton", u"OK", None))
    #retranslateUi
  1. Implementation of reset function function:
  • Define QButtonGroup and add QRadioButton, add new properties to the QCheckBox component and a function to be called after clicking.
 def resetRadioButton(self):
        #reset radiobutton state, if select the same.
        #1. create QButtonGroup
        self.buttonGroup_CheckOption1 = QButtonGroup()
        self.buttonGroup_CheckOption2 = QButtonGroup()
        #2. add radiobutton to group
        self.buttonGroup_CheckOption1.addButton(self.radioButton_Left_Option1)
        self.buttonGroup_CheckOption1.addButton(self.radioButton_Left_Option2)
        self.buttonGroup_CheckOption2.addButton(self.radioButton_Right_Option1)
        self.buttonGroup_CheckOption2.addButton(self.radioButton_Right_Option2)
        # 3. add new property for boot group
        self.checkBox_Option1.setProperty('buttonGroup','CheckOption1')
        self.checkBox_Option2.setProperty('buttonGroup','CheckOption2')
        #4. reset radiobutton by deselect checkbox
        self.checkBox_Option1.clicked.connect(self.changeRadioButtonState)
        self.checkBox_Option2.clicked.connect(self.changeRadioButtonState)
  • Define slot function:
    a. Obtain the signal sending source through the method sender() to know which QCheckBox sent the signal and its selected state
    b. Get the value of the custom property ‘buttonGroup’ through the property method, and you can get the object name of QButtonGroup.
    c. Obtain all QRadioButtons through the buttons method of QButtonGroup
    d. By canceling the mutual exclusive state ‘setExclusive(False)’, then setting all QRadioBoxes to the unselected state, and finally reactivating the mutual exclusive state.
 def changeRadioButtonState(self,state):
        hdCheckBox = self.sender()
        print('hdCheckBox:{}, state:{}'.format(hdCheckBox.objectName(),state))
        strButtonGroup = hdCheckBox.property('buttonGroup')
        buttonList = eval(r'self.buttonGroup_' + strButtonGroup + '.buttons()')
        if not state:
            eval(r'self.buttonGroup_' + strButtonGroup + '.setExclusive(False)')
            for radionButton in buttonList:
                radionButton.setChecked(state)
            eval(r'self.buttonGroup_' + strButtonGroup + '.setExclusive(True)')
  1. Complete code:
from PySide6.QtWidgets import QMainWindow, QApplication, QButtonGroup
from PySide6.QtCore import QCoreApplication
from testQRadioButton_ui import Ui_testRadioButton
importsys


class pySideFileDialog(QMainWindow, Ui_testRadioButton):
    def __init__(self, parent = None) -> None:
        super(pySideFileDialog,self).__init__(parent)
        self.setupUi(self)
        self.resetRadioButton()
        self.actionUI()
    
    
    def resetRadioButton(self):
        #reset radiobutton state, if select the same.
        #1. create QButtonGroup
        self.buttonGroup_CheckOption1 = QButtonGroup()
        self.buttonGroup_CheckOption2 = QButtonGroup()
        #2. add radiobutton to group
        self.buttonGroup_CheckOption1.addButton(self.radioButton_Left_Option1)
        self.buttonGroup_CheckOption1.addButton(self.radioButton_Left_Option2)
        self.buttonGroup_CheckOption2.addButton(self.radioButton_Right_Option1)
        self.buttonGroup_CheckOption2.addButton(self.radioButton_Right_Option2)
        # 3. add new property for boot group
        self.checkBox_Option1.setProperty('buttonGroup','CheckOption1')
        self.checkBox_Option2.setProperty('buttonGroup','CheckOption2')
        #4. reset radiobutton by deselect checkbox
        self.checkBox_Option1.clicked.connect(self.changeRadioButtonState)
        self.checkBox_Option2.clicked.connect(self.changeRadioButtonState)

    
    def actionUI(self):
        self.pushButton_QRadioOK.clicked.connect(self.doSomething)
        self.pushButton_ClosedWindow.clicked.connect(self.closeWindow)
    
    def changeRadioButtonState(self,state):
        hdCheckBox = self.sender()
        print('hdCheckBox:{}, state:{}'.format(hdCheckBox.objectName(),state))
        strButtonGroup = hdCheckBox.property('buttonGroup')
        buttonList = eval(r'self.buttonGroup_' + strButtonGroup + '.buttons()')
        if not state:
            eval(r'self.buttonGroup_' + strButtonGroup + '.setExclusive(False)')
            for radionButton in buttonList:
                radionButton.setChecked(state)
            eval(r'self.buttonGroup_' + strButtonGroup + '.setExclusive(True)')
            
    def doSomething(self):
        print('do something!')
            
    def closeWindow(self):
        QCoreApplication.quit()
    
    
    
if __name__=='__main__':
    app = QApplication(sys.argv)
    window = pySideFileDialog()
    window.show()
    sys.exit(app.exec_())