在PyQt Python ui文件中抓取所有QPushButton [英] Grabbing all QPushButton in a PyQt Python ui File

查看:219
本文介绍了在PyQt Python ui文件中抓取所有QPushButton的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用带有大量QPushButton的Qt Designer创建了一个UI文件,然后使用pyuic4将其转换为python文件.

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

我想将所有QPushButtons添加到QButtonGroup.

I want to add all the QPushButtons to a QButtonGroup.

如何迭代或获取我所有的QPushButton,以从我的UI Python文件添加到QButtonGroup中?

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

推荐答案

在Qt Designer中,将所有按钮都放在容器小部件中.

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

然后,您可以使用 findChildren 遍历所有子按钮.因此,如果self.buttonBox是您的容器小部件,则可以执行以下操作:

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)

这篇关于在PyQt Python ui文件中抓取所有QPushButton的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆