Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why in pyqt5 should I use pyuic5 and not uic.loadUi("my.ui")?

Tags:

python

pyqt

pyqt5

I've been experimenting with QT5 for Python, using pyqt5. I've noticed that most tutorials recommend using pyuic5 to convert the XML UI to Python code. I've also seen a couple of tutorials where instead they use uic.loadUi("myui.ui") to dynamical load the XML UI. This seems like a cleaner, more modular solution to me, but it appears to be an unpopular option. Is there a reason converting your code with pyuic5 is a sounder solution?

like image 621
Frank Avatar asked Sep 24 '18 01:09

Frank


People also ask

What is UIC in PYQT?

The uic module contains classes for handling the . ui files created by Qt Designer that describe the whole or part of a graphical user interface. It includes classes that load a . ui file and render it directly, and classes that generate Python code from a .

How do I link a python code to UI?

Importing the UI File In Python. First we need to import the modules required. We need QtWidgets from PyQt5 for the base widget and uic from PyQt5 also to load the file. We also need sys to access arguments.

How to generate the Ui file in PyQt5?

Generating the UI File As covered in my original PyQt5 tutorial, install the designer, locate it and use it. When saving the GUI you have created, it will be saved as a .ui. This .ui file is XML that contains the layout of the GUI and other things you may have set in the designer application. Here is a snippet of an example .ui file:

What is the difference between PyQt5 and pyuic5?

The pyuic5 script is calling the python installed under /usr, whereas you installed PyQt5 under /usr/local. Whenever you build and install packages, you must always run the configuration scripts using the python executable for the specific python installation you are targeting. Thanks for the answer from @Akhil.

Why should I use pyuic5?

The biggest reason to use pyuic5 is that IDEs (e.g. Visual Studio Code, PyCharm) do not understand .ui files, and therefore cannot provide code introspection (e.g. autocompletion of widget names) for your class unless you compile it to Python. Thanks for contributing an answer to Stack Overflow!

Why is my PyQt5 import not working?

A small check-list for things that may go wrong: PyQt5 isn't installed The .ui file you are importing does not exist (incorrect reference) You did not inherit the correct class (found in the XML) Getting Widget Object Pointers Once you have the GUI being imported, you now need to identify some pointers for the objects you want to use.


3 Answers

Both solutions are good, they have advantages and disadvantages that have to be weighed with what you want to do, and many times it will depend on the taste of the programmer.

pyuic5:

  • Allows inheritance [+]

  • There is no additional load when running the application [+]

  • Convert the .ui to .py every time the file is modified [-]

uic.loadUi():

  • You do not have to modify anything when modifying the .ui [+]

  • Compilation extra time [+]

  • Does not allow inheritance (You could implement the inheritance using uic.loadUiType()) [-]

  • Does not allow the use of inspect [-].

like image 104
eyllanesc Avatar answered Oct 27 '22 15:10

eyllanesc


The biggest reason to use pyuic5 is that IDEs (e.g. Visual Studio Code, PyCharm) do not understand .ui files, and therefore cannot provide code introspection (e.g. autocompletion of widget names) for your class unless you compile it to Python.

like image 38
Jussi Nurminen Avatar answered Oct 27 '22 14:10

Jussi Nurminen


Having looked into this more, for me dynamically loading the form generated by QT Designer is the way to go. You can get round the problems of not having access to the underlying widgets with commands like this (assuming you have a label called bob_label in your form):

self.bob_label: QLabel = self.findChild(QtWidgets.QLabel, 'bob_label')

In PyCharm, at any rate, this will automatically enable Intellisense (so the IDE now knows that there's a label in the window class called bob_label). In Visual Studio Code despite great effort I couldn't find a way to get this to work easily, although I did manage it eventually.

like image 1
Andy Brown Avatar answered Oct 27 '22 15:10

Andy Brown