16

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
ekhumoro
  • 115,249
  • 20
  • 229
  • 336
anils
  • 1,782
  • 6
  • 19
  • 29

2 Answers2

33

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

ekhumoro
  • 115,249
  • 20
  • 229
  • 336
  • @ekhumoro could you please help me here : http://stackoverflow.com/questions/25164853/how-to-use-findchildren#25165738 – Ejaz Aug 06 '14 at 19:01
-4

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*>();
Dmitriy
  • 5,357
  • 8
  • 45
  • 57