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?
-
You could also just use straight pyqt5 to create your windows and steer completely clear of that UI generator and personally this works a whole lot better as you have full control over the code you create and you avoid that extremely ugly code the generator/designer creates. Oh and its already in python no conversion necessary – Dennis Jensen Jun 28 '19 at 16:32
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 [-].

- 2,276
- 1
- 16
- 24

- 235,170
- 19
- 170
- 241
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.

- 2,257
- 1
- 9
- 16
-
2This is what I suspected. Using Visual Studio Code, and just learning about Qt. I don't really care how messy the code generated is - I'll never see it - but I do want intellisense to pop up. Sadly, this means having to convert the UI to Python every time I make a change. – Andy Brown Jun 01 '22 at 10:28
-
@AndyBrown if you really hate the manual compilation step, I guess you could rig your Python code to autocompile the .uic file whenever it has changed. This could be done e.g. by comparing the modification dates on the .uic and .py files - if the .uic is newer, then recompile to .py before import. Of course you would only do this during development, when the .ui is expected to change frequently. – Jussi Nurminen Jun 02 '22 at 07:29
-
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.

- 5,309
- 4
- 34
- 39