How to get all child components of QWidget in pyside/pyqt/qt?

放肆的年华 提交于 2019-11-29 13:39:08

问题


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

回答1:


The signatures of findChild and findChildren 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 the objectName).

So your example should look something like this:

lineEdits = form.findChildren(QtGui.QLineEdit)

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).




回答2:


Use this method QObject::findChildren(onst QString & name = QString()) with no parameters.

Omitting the name argument causes all object names to be matched.

Here is C++ example code:

QList<QLineEdit*> line_edits = form.findChildren<QLineEdit*>();


来源:https://stackoverflow.com/questions/9395535/how-to-get-all-child-components-of-qwidget-in-pyside-pyqt-qt

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!