PySide6 QRadioButton 状态重置
通过QCheckBox取消选中实现QRadioButton状态重置。
原理介绍
通过QBottonGroup 抽象的按钮容器,实现QRadioButton组件互斥。之后通过给QCheckBox按钮添加自定义属性,以便获取QButtonGroup的对象名。在通过QButtonGroup对象名找到添加在其内的QRadioButton,然后重置QRadioButton。
- 创建如下GUI界面:
GUI 界面代码如下 :
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 {\n"
"border: 0px;\n"
"}")
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 {\n"
"background-color: transparent;\n"
"border: 1px solid silver;\n"
"border-radius: 6px;\n"
"margin-top: 6px;\n"
"}\n"
"\n"
"QGroupBox::title {\n"
"subcontrol-origin: margin;\n"
"left: 12px;\n"
"padding: 0px 5px 0px 5px;\n"
"}")
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
- 重置功能函数的实现:
- 定义QButtonGroup并添加QRadioButton,给QCheckBox组件添加新属性和点击后调用的函数。
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)
- 定义槽函数:
a. 通过方法sender()获取信号发送源,以此知道是哪个QCheckBox发送的信号及其选中状态state
b. 通过property方法获取自定义属性’buttonGroup’的值,可以得到QButtonGroup的对象名。
c. 通过QButtonGroup的buttons方法获得所有的QRadioButton
d. 通过取消互斥状态’setExclusive(False)', 然后设置所有QRadioBox为非选中状太,最后重新激活互斥状态。
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)')
- 完整代码:
from PySide6.QtWidgets import QMainWindow, QApplication, QButtonGroup
from PySide6.QtCore import QCoreApplication
from testQRadioButton_ui import Ui_testRadioButton
import sys
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_())