问题描述
我正在使用pyside(qt)开发一个桌面应用程序,我想访问(迭代)QWidget的所有行编辑组件。在qt中,我发现了两个方法 findChild 和 findChildren ,但是没有找到合适的示例,并且我的代码显示错误,'form'对象没有属性'findChild'。
这里的窗体是Qwidget窗体,由行编辑,组合框,Qpushbuttons等组成。
I am developing a desktop application using pyside(qt), I want to access(iterate) all line edit components of QWidget. In qt I found two methods findChild and findChildren but there is no proper example found and My code shows error, 'form' object has no attribute 'findChild'.Here 'form' is Qwidget form consist components lineEdit, comboboxes, Qpushbuttons etc.
代码:
lineEdits = form.findChild<QLineEdit>() //This is not working
lineEdits = form.findChild('QLineEdit) //This also not working
推荐答案
<$ c $的签名在PySide / PyQt4中,c> findChild 和 findChildren
有所不同,因为在Python中没有真正等同于C ++强制转换语法的
The signatures of findChild
and findChildren
are different in PySide/PyQt4 because there is no real equivalent to the C++ cast syntax in Python.
相反,您必须传递一个类型(或 tuple
类型)作为第一个参数,并传递一个可选字符串作为第二个参数(用于匹配 objectName
)。
Instead, you have to pass a type (or tuple
of types) as the first argument, and an optional string as the second argument (for matching the objectName
).
所以您的示例应如下所示:
So your example should look something like this:
lineEdits = form.findChildren(QtGui.QLineEdit)
请注意, findChild
和 findChildren
是 QObject
的方法-因此,如果您的表单没有它们,则不能为 QWidget
(因为所有小部件都继承 QObject
)。
Note that findChild
and findChildren
are methods of QObject
- so if your form does not have them, it cannot be a QWidget
(because all widgets inherit QObject
).
这篇关于如何在pyside / pyqt / qt中获取QWidget的所有子组件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!