I think the best way to determine which button is checked is to use a QButtonGroup , as it provides a container for organizing groups of button widgets. This is not a visual object, therefore, it does not replace the layout for the visual arrangement of your switches, but it allows you to make them mutually exclusive and associate the whole "id" with them, so you know which one is checked without having to repeat all the widgets present in the layout.
If you decide to use it, your code should look something like this:
self.wPaymantType.qgbSomeSelectionGroup = QtGui.QGroupBox() vbox = QtGui.QVBoxLayout() radioGroup = QtGui.QButtonGroup() radioGroup.setExclusive(True) for i,row in enumerate(listOfChoices): radio = QtGui.QRadioButton(row) radioGroup.addButton(radio, i) if bIsFirst: radio.setChecked(True) bIsFirst = False if len(row.name) > nMaxLen: nMaxLen = len(row.name) vbox.addWidget(radio) self.wPaymantType.qgbSomeSelectionGroup.setLayout(vbox)
To identify the checked button, you can use the QButtonGroup checked method:
buttonId = radioGroup.checkedId()
or if you want to extract the button object itself, you can use the checkedButton method:
button = radioGroup.checkedButton()
source share