[PyQt] Confusion about load.uic() and import regarding pyinstaller

Maurizio Berti maurizio.berti at gmail.com
Sun Mar 10 16:21:07 GMT 2019

Importing the pyuic generated file is not exactly the same as using
The imported ui object is not a PyQt QObject, but a standard Python object
derived class that contains references to the GUI objects once it's setup
onto the Qt base class.
As you can see in the PyQt Designer documentation (
http://pyqt.sourceforge.net/Docs/PyQt5/designer.html ), subclassing the
original class type only (QMainWindow, QDialog, etc) requires you to
instanciate the UI object, which is a Python object that is able to "build"
the GUI "inside" the subclassed widget object. By using the multiple
inheritance method, the UI object is already "built in" the class instance,
and that's why you can access the ui objects directly, exactly as you can
do after using loadUi().

The setWindowTitle and findChildren are methods of the base Qt class
(QMainWindow/QWidget, and QObject) you are inheriting from, while the
"form" class contains all references to the objects of the interface
(widgets and actions) that are built *into* the base Qt class, and that's
why you can access them only from the self.ui interface.
What loadUi does is to build the GUI into the main widget as setupUI does,
while also setting widgets and actions as the main class attributes as it
happens with the multiple inheritance system.

Hope this might clear up things (if anybody reads some misconceptions and
mistakes in my comprehension of PyQt ui interface, I'd be happy to be
corrected :-) )

Il giorno dom 10 mar 2019 alle ore 15:43 Hans Jörg Maurer <hjm at pmeonline.net>
ha scritto:

> @michael h @Barry:
> Thanks. But indeed you have to decide which way to go. In the meantime I
> found a workaround:
> The directory of the ui - File must be include in the pyprogramfile.spec
> as pathex=nameofui.ui or as hiddenimport on the command line of pyinstaller.
> Nethertheless it is confusing that there are different behaviors if a ui
> is loaded as file or imported. In my opinion there should be no difference.
> Maybe there are more troublemakers...
> Regards
> Hans
>> Both did run, but if I use the import and set e.g.
>> "self.ui.setWindowTitle('Do not read my title')" the app crashes with
>> "AttributeError: 'Ui_MainWindow' object has no attribute 'setWindowTitle'"
>> I don't understand the reason and in addition I have no idea how to solve
>> this. Further ith hangs on
>>  widgetList = self.ui.findChildren (QPushButton)
>> AttributeError: 'Ui_MainWindow' object has no attribute 'findChildren'
>> The idea behind was that I compile the ui before I run pyinstaller to
>> avoid problems at runtime.
> You need to call self.setWindowTitle or self.findChildren. Those are
> methods on QMainWindow.
> _______________________________________________
> PyQt mailing list    PyQt at riverbankcomputing.com
> https://www.riverbankcomputing.com/mailman/listinfo/pyqt

È difficile avere una convinzione precisa quando si parla delle ragioni del
cuore. - "Sostiene Pereira", Antonio Tabucchi
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://www.riverbankcomputing.com/pipermail/pyqt/attachments/20190310/d50dd9e5/attachment.html>

More information about the PyQt mailing list