On a selection check false i try to prevent the selection of that radiobutton. But it does get selected anyway, even with singleshot timer:
class MyQWidget(QWidget):
def __init__(self):
QWidget.__init__(self)
...
self.ui.buttonGroup.buttonPressed.connect(self.is_init)
def is_init(self, button):
if True: #some check here to prevent selection of it
print('no select')
QTimer.singleShot(0, lambda: button.setChecked(False))
EDIT:
Assume a button group, i try to intercept the press call (not clicked) to not select that radio button if a custom test is false (if True: #some check
), but i cannot prevent the button from being selected also i set it to False
, how do abort the selection of the clicked/pressed radio button if a condition is not met?
The problem is caused by:
A QButtonGroup
is by default exclusive
which implies that a button will always be selected causing the setChecked(False)
not to work since it would imply having no button pressed, so there is to enable and disable that property in the change.
The change of state does not occur in the pressed but in the released so you must use the buttonReleased
signal.
Considering the previous one in the next part I show a MWE:
from PySide2 import QtCore, QtWidgets
class Widget(QtWidgets.QWidget):
def __init__(self, parent=None):
super().__init__(parent)
self.buttonGroup = QtWidgets.QButtonGroup(self)
self.buttonGroup.buttonReleased.connect(self.on_buttonReleased)
lay = QtWidgets.QHBoxLayout(self)
for i in range(4):
button = QtWidgets.QRadioButton(f"button-{i}")
lay.addWidget(button)
self.buttonGroup.addButton(button)
@QtCore.Slot(QtWidgets.QAbstractButton)
def on_buttonReleased(self, button):
if True:
self.buttonGroup.setExclusive(False)
button.setChecked(False)
self.buttonGroup.setExclusive(True)
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
w = Widget()
w.resize(640, 240)
w.show()
sys.exit(app.exec_())