jeudi 3 mai 2018

How to iterate over QButtonGroup object in PyQt4?

I am attempting to set all QCheckBoxes that have been added to a QButtonGroup object to setChecked(True). I have the following code in PyQt4:

import sys
from PyQt4.QtCore import *
from PyQt4.QtGui import *

class checkdemo(QWidget):
   def __init__(self, parent = None):
      super(checkdemo, self).__init__(parent)

      layout = QHBoxLayout()
      self.b1 = QCheckBox("Button1")
      #self.b1.setChecked(True)
      #self.b1.stateChanged.connect(lambda:self.btnstate(self.b1))
      layout.addWidget(self.b1)

      self.b2 = QCheckBox("Button2")
      #self.b2.toggled.connect(lambda:self.btnstate(self.b2))
      layout.addWidget(self.b2)

      self.setLayout(layout)
      self.setWindowTitle("checkbox demo")

      group = QButtonGroup()
      group.addButton(self.b1)
      group.addButton(self.b2)
      print group.children()

      #print help(group)
      #for button in group:
      #    button.setChecked(True)

The children() method returns an empty list, and the For loop at the end results in this error: TypeError: 'QButtonGroup' object is not iterable, so this solution does not work.

Does anyone know how set all QCheckBox objects to True/False in a given GUI?




Aucun commentaire:

Enregistrer un commentaire