Finding checked QRadioButton among many into a QVBoxLayout
I used the code below to dynamically create a group of radio buttons:
self.wPaymantType.qgbSomeSelectionGroup = QtGui.QGroupBox()
vbox = QtGui.QVBoxLa开发者_开发百科yout()
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 through all radio buttons to find out which one is checked?
I tried something like this, but I didn't get anything good from it:
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"
Your code is not minimal and self-contained, so it's really hard to help you -- but I've anyway gone through the effort of building a near-minimal self-contained approximation of what you're trying to do and which does seem to work correctly -- here comes...:
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()
This seems to do what you want. Key change is to keep the actual Python widget objects around rather than trying to restore them from the layout vbox -- that attempt seems to not be working as intended, at least as regards correct access to the crucial detail about whether a given radio button is checked or not, which is of course the heart of your Q.
I believe the reason why it's not working is your
radio = QtGui.QRadioButton(child.widget())
call at the code where you're checking if your checkbox is checked. I think what you're trying to do is typecast the child object to QtGui.QRadioButton and it doesn't work in this case. Instead you should be creating a new widget. Try changing it to smth. like this:
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 code above should be iterating through child objects of the layout object, check their type and print "radio button..." in case it's radio buttong and it's checked
hope this helps, regards
I guess a better way to identify which button is checked is to use a QButtonGroup, as it provides a container to organize groups of button widgets. It is not a visual object, so it doesn't substitute the layout for visually arranging your radio buttons, but it does allow you to make them mutually exclusive and associate an integer "id" with them, letting you know which one is checked without the need to iterate through all the widgets present in the layout.
If you decide to use it, your code should turn into 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 QButtonGroup's checkedId method:
buttonId = radioGroup.checkedId()
or if you want to retrive the button object itself you can use the checkedButton method:
button = radioGroup.checkedButton()
精彩评论