Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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
like image 234
anils Avatar asked Feb 22 '12 13:02

anils


People also ask

How do you get children of Qwidget?

You can use the findChild() function with the object name to get a specific child. You can also use findChildren() to get all the children that have the same name and then iterate through the list using foreach() or QListIterator . how do I use the QListIterator?

Is PySide better than PyQt?

PySide is LGPL while PyQt is GPL. This could make a difference if you don't wish to make your project opensource. Although PyQt always has the propriety version available for a fairly reasonable price. I tend to find the PySide documentation more intuitive.

Why use PySide instead of PyQt?

Advantages of PySide PySide represents the official set of Python bindings backed up by the Qt Company. PySide comes with a license under the LGPL, meaning it is simpler to incorporate into commercial projects when compared with PyQt. It allows the programmer to use QtQuick or QML to establish the user interface.

Is PySide and PyQt the same?

The short answer is that the reason lies mainly in license related issues and that PyQt and PySide are actually very similar, so similar that the code below for a QT based version of the miles-to-kilometers converter works with both PyQt and PySide. For PySide you only have to replace the import line at the beginning.


1 Answers

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

like image 153
ekhumoro Avatar answered Oct 13 '22 19:10

ekhumoro