Checkbox To Uncheck All Other Checkboxes
I'm trying to put in some validation such that: When either 'Select A' or 'Select B' is checked, 'None Selected' is automatically unchecked When 'None Selected' is checked, both '
Solution 1:
The problem is caused because according to your requirements, you should only make those options if some QCheckBox
is checked but you do not do that verification, to be able to handle it properly, create a slot and to know which object the signal was emited, the sender()
method:
import sys
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
class Test(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
self.checkBoxNone = QCheckBox("None Selected")
self.checkBoxA = QCheckBox("Select A")
self.checkBoxB = QCheckBox("Select B")
self.checkBoxNone.setChecked(True)
self.checkBoxNone.stateChanged.connect(self.onStateChange)
self.checkBoxA.stateChanged.connect(self.onStateChange)
self.checkBoxB.stateChanged.connect(self.onStateChange)
grid = QGridLayout(self)
grid.addWidget(self.checkBoxNone, 1, 0)
grid.addWidget(self.checkBoxA, 2, 0)
grid.addWidget(self.checkBoxB, 3, 0)
self.setWindowTitle('Test')
self.show()
@pyqtSlot(int)
def onStateChange(self, state):
if state == Qt.Checked:
if self.sender() == self.checkBoxNone:
self.checkBoxA.setChecked(False)
self.checkBoxB.setChecked(False)
elif self.sender() in (self.checkBoxA, self.checkBoxB):
self.checkBoxNone.setChecked(False)
Post a Comment for "Checkbox To Uncheck All Other Checkboxes"