سؤال

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?

هل كانت مفيدة؟

المحلول

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)
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top