[PyQt] pyuic4 vs uic Module

Kyle Altendorf sda at fstab.net
Thu Aug 2 21:01:42 BST 2018


On 2018-08-02 15:04, Christopher Probst wrote:

> Thank-you very much for your insight. I must say, coming from the Qt 
> C++ world, dynamically loading the ui at run-time seems unusual to me. 
> Is there a performance hit? Why not use pyqt4uic to compile in the code 
> into Python? What is the issue with that?

Compiling statically would be the only build step so it's not like in 
C++ where you would just add it to an existing build system.  I'm sure 
there's some performance hit but I believe the rest of my program is 
likely much slower than parsing a little xml and traversing the 
resulting tree.  Also, if you use the `uic.loadUiType()` approach (as 
opposed to `uic.loadUi()`) then you only need to do it once at import of 
a module rather than each time you create an instance of that ui 
element.  So, no GUI lag at all, just a 'little' extra startup time.

But I'm sure that many people do happily use pyuic.

I was just chatting in #pyqt with someone about the same topic.  They 
were compiling right before import using uic.compileUi() so as to have 
actual .py files for the sake of IDE completion features.  I mentioned 
having considered making a Python import hook to catch and handle things 
like `import mywindow_ui` and do a build of `mywindow.ui` to 
`mywindow_ui.py` behind the scenes.  Not sure that 'magic' would really 
be worthwhile, but maybe I'll do it for fun at some point.

Cheers,
-kyle


> On Wed, 1 Aug 2018 at 17:24, Kyle Altendorf <sda at fstab.net> wrote:
> 
>> On August 1, 2018 5:08:48 PM EDT, Christopher Probst 
>> <christop.probst at gmail.com> wrote:
>>> Helllo everybody,
>>> 
>>> Is there any best practice suggestion as to how to interact with the 
>>> Qt
>>> ui
>>> files? Is it better to load them dynamically using the uic module 
>>> with
>>> the
>>> load uic.loadUi method?
>>> 
>>> Or is it recommended to compile the ui files into python code using
>>> pyuic4?
>> 
>> I'm not sure any caveats between 4 vs 5 but I much prefer loading at 
>> run time.  Here's what I'm tending towards now.
>> 
>> https://github.com/altendky/basicpyqt5example/blob/e08e75d16819fddc0514c513f43fa5148e59722c/src/basicpyqt5example/mainwindow.py#L37
>> 
>> Ui, UiBase = PyQt5.uic.loadUiType(
>> pathlib.Path(__file__).parents[0] / 'mainwindow.ui',
>> )
>> 
>> class MainWindow(UiBase):
>> def __init__(self, parent=None):
>> super().__init__(parent) self.ui = Ui()
>> self.ui.setupUi(self)
>> 
>> The one caveat that I'm aware of is the lack of a .py file for an IDE 
>> to parse for help completing names.  I'll note that there is a call in 
>> uic for compiling without having to run pyuic from the command line so 
>> some hybrid might make sense.
>> 
>> Cheers,
>> -kyle


More information about the PyQt mailing list