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

后端 未结 2 1850
春和景丽
春和景丽 2020-12-09 19:58

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 find

相关标签:
2条回答
  • 2020-12-09 20:36

    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*>();
    
    0 讨论(0)
  • 2020-12-09 20:47

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

    0 讨论(0)
提交回复
热议问题