如何获取pyside/pyqt/qt中QWidget的所有子组件?
我正在使用 pyside(qt) 开发桌面应用程序,我想访问(迭代)QWidget 的所有行编辑组件。在qt中,我发现了两个方法findChild和findChildren,但没有找到正确的示例,并且我的代码显示错误,'form'对象没有属性'findChild'。 这里的'form'是Qwidget表单,由lineEdit、组合框、Qpushbuttons等组件组成。
代码:
lineEdits = form.findChild<QLineEdit>() //This is not working
lineEdits = form.findChild('QLineEdit) //This also not working
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.
Code:
lineEdits = form.findChild<QLineEdit>() //This is not working
lineEdits = form.findChild('QLineEdit) //This also not working
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
PySide/PyQt4 中
findChild
和findChildren
的签名不同,因为 Python 中没有与 C++ 转换语法真正等效的语法。相反,您必须传递一个类型(或类型的
元组
)作为第一个参数,并传递一个可选字符串作为第二个参数(用于匹配objectName
)。因此,您的示例应如下所示:
请注意,
findChild
和findChildren
是QObject
的方法 - 因此,如果您的表单没有它们,则它不能是QWidget
(因为所有小部件都继承QObject
)。The signatures of
findChild
andfindChildren
are different in PySide/PyQt4 because there is no real equivalent to the C++ cast syntax in Python.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 theobjectName
).So your example should look something like this:
Note that
findChild
andfindChildren
are methods ofQObject
- so if your form does not have them, it cannot be aQWidget
(because all widgets inheritQObject
).使用此方法 QObject::findChildren(onst QString & name = QString ()) 不带参数。
这是 C++ 示例代码:
Use this method QObject::findChildren(onst QString & name = QString()) with no parameters.
Here is C++ example code: