Question

I created a UI File using Qt Designer with lots of QPushButtons, and then I converted it into a python file using pyuic4.

I want to add all the QPushButtons to a QButtonGroup.

How do I iterate or grab all my QPushButtons to add in to a QButtonGroup from my UI Python file?

Was it helpful?

Solution

In Qt Designer, put all your buttons inside a container widget.

You can then use findChildren to iterate over all the child buttons. So if self.buttonBox was your container widget, then you could do something like:

self.buttonGroup = QtGui.QButtonGroup(self)
for button in self.buttonBox.findChildren(QtGui.QAbstractButton):
    self.buttonGroup.addButton(button)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top