Search for a proven QRadioButton among many in QVBoxLayout

I used the code below to dynamically create a group of radio buttons:

self.wPaymantType.qgbSomeSelectionGroup = QtGui.QGroupBox() vbox = QtGui.QVBoxLayout() for row in listOfChoices: radio = QtGui.QRadioButton(row) if bIsFirst: radio.setChecked(True) bIsFirst = False if len(row.name) > nMaxLen: nMaxLen = len(row.name) vbox.addWidget(radio) self.wPaymantType.qgbSomeSelectionGroup.setLayout(vbox) 

How can I iterate over all the switches to find out which one is checked?

I tried something like this, but it didn't work out well for me:

 qvbl = self.qgbSomeSelectionGroup.children()[0] for i in range(0, qvbl.count()): child = qvbl.itemAt(i) radio = QtGui.QRadioButton(child.widget()) if radio != None: if radio.isChecked(): print "radio button num " + str(i) + " is checked" 
+2
source share
3 answers

I believe the reason it doesn't work is yours

  radio = QtGui.QRadioButton(child.widget()) 

call the code in which you check if your checkbox is checked. I think what you are trying to do is create a child object for QtGui.QRadioButton, in which case it does not work. Instead, you should create a new widget. Try changing it to smth. eg:

  qvbl = self.qgbSomeSelectionGroup.layout() for i in range(0, qvbl.count()): widget = qvbl.itemAt(i).widget() if (widget!=0) and (type(widget) is QtGui.QRadioButton): if widget.isChecked(): print "radio button num " + str(i) + " is checked" 

the above code should be iterated through the child objects of the layout object, check their type and print the "radio button ..." if it is a radio station, and it checked

hope this helps, believes

+1
source

Your code is not minimal and self-sufficient, so itโ€™s very difficult for you to help, but I still tried to build an almost minimal autonomous approximation of what you are trying to do, and which seems to work correctly - and it turns out ...:

 from PyQt4 import QtGui import sys class MainWindow(QtGui.QMainWindow): def __init__(self, parent=None): super(MainWindow, self).__init__(parent) self.dowid() self.setCentralWidget(self.thewid) def dowid(self): self.thewid = QtGui.QGroupBox() vbox = QtGui.QVBoxLayout() self.radiobuttons = [] listOfChoices = 'one two three'.split() for i, row in enumerate(listOfChoices): radio = QtGui.QRadioButton(row) if i == 0: radio.setChecked(True) self.radiobuttons.append(radio) vbox.addWidget(radio) self.thewid.setLayout(vbox) def examine(self): for i, radio in enumerate(self.radiobuttons): if radio.isChecked(): print "radio button num " + str(i) + " is checked" else: print "radio button num " + str(i) + " is NOT checked" if __name__ == '__main__': app = QtGui.QApplication([]) mainWin = MainWindow() mainWin.show() rc = app.exec_() mainWin.examine() 

It looks like what you want. The key change is saving the Python virtual widget objects, rather than trying to restore them from the vbox layout - this attempt does not seem to work properly, at least with respect to the correct access to important information about whether this switch is checked or not, which, of course, is the heart of your Q.

+3
source

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() 
0
source

All Articles