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

Hans Jörg Maurer hjm at pmeonline.net
Tue Mar 12 19:28:08 GMT 2019


Here the link to the SO Question mentioned in the mail below:
https://stackoverflow.com/questions/46515179/subclassing-of-widgets-which-are-loaded-by-uic-loadui





 Thanks for the clearing. It's not totaly clear to me, I am still on
the learning curve. I have to go through it several times.

As I read your answer I remember that I had some problems with a
ListView and Drag and Drop. I had to subclass it to run. Unfortunally
I don't find the link to the stackoverflow answer which helped me to
solve. If I get it back I'll put it to this thread. This project is
also based on loadUic.



Regards
Hans


Am Sonntag, den 10.03.2019 um 17:21 schrieb Maurizio Berti:


Importing the pyuic generated file is not exactly the same as using
loadUi().
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 :-) )
Maurizio





Il giorno dom 10 mar 2019 alle ore 15:43 Hans Jörg Maurer  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 [1]
https://www.riverbankcomputing.com/mailman/listinfo/pyqt






-- 
È difficile avere una convinzione precisa quando si parla delle
ragioni del cuore. - "Sostiene Pereira", Antonio Tabucchi
http://www.jidesk.net


 



Links:
------
[1] mailto:pyQt at riverbankcomputing.com
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://www.riverbankcomputing.com/pipermail/pyqt/attachments/20190312/0b4a6068/attachment.html>


More information about the PyQt mailing list