tags:

views:

71

answers:

2

Hi all,

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

self.wPaymantType.qgbSomeSelectionGroup = QtGuig.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 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"

Any help will be appreciated!

Bye

A: 

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.

Alex Martelli
Thank you very much for your effort. I'll try this later.
Tiho
A: 

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

serge_gubenko
Thanks,this look's like it could work. I appreciate your help!
Tiho