[PyQt] Using ui files in PyQt

Hans-Peter Jansen hpj at urpla.net
Thu Apr 27 20:38:27 BST 2017


Hi Mike,

On Donnerstag, 27. April 2017 12:46:31 Mike Driscoll wrote:
> Hi,
> 
> At my current job, one of our major applications in written using Qt (the
> C++ version). Most of the UI is done in Qt Creator so we have a lot of *.ui
> files. Our company supports having our engineers do hack-a-thons and I was
> thinking it would be cool if I could create a little prototyping framework
> using PyQt around the ui files.

Shameless ad for the "framework": https://pypi.python.org/pypi/distutils_ui

This is a distutils extension, exactly targeted on handling the auxiliary 
resources, that need to be dealed with in PyQt.

Here's an example run (with comments):
$ python3 setup.py build_ui --force
running build_ui

# create a project file

running gentrpro
gentrpro.gentrpro: "main.pro" generated

# extract translations

running pylupdate
pylupdate.command: pylupdate5 -verbose {infiles}
pylupdate.run_command: "pylupdate5 -verbose main.pro"
Updating 'main_de.ts'...
    Found 65 source texts (0 new and 65 already existing)

# generate binary translation file(s)

running lrelease
lrelease.run_command: "lrelease-qt5 main.pro"
Info: creating stash file 
/home/hp/src/GIT/kde_color_scheme_editor/src/i18n/.qmake.stash
Updating '/home/hp/src/GIT/kde_color_scheme_editor/src/i18n/main_de.qm'...
    Generated 65 translation(s) (65 finished and 0 unfinished)

# generate python modules from ui files

running pyuic
pyuic.run_command: "pyuic5 -x -o src/ui/ui_mainwin.py src/ui/mainwin.ui"
pyuic.run_command: "pyuic5 -x -o src/ui/ui_complementdialog.py 
src/ui/complementdialog.ui"
pyuic.run_command: "pyuic5 -x -o src/ui/ui_aboutdialog.py 
src/ui/aboutdialog.ui"

# generate resource description

running genqrc
genqrc.genqrc: "main.qrc" generated

# generate resource module

running pyrcc
pyrcc.run_command: "pyrcc5 -o src/main_rc.py src/main.qrc"


Now, you have a main_rc.py file containing images, translations and auxiliary 
files, and a couple of ui python modules.

You import the main_rc module once (early in your process):

import main_rc  # __IGNORE_WARNING__

Now, all included files are available the usual way:

E.g.:

    locale = QLocale()
    if locale.name() != "C":
        translator = QTranslator()
        if translator.load(locale, ':/i18n/main', '_', 'i18n'):
            app.installTranslator(translator)
            log.info('translation %s installed', locale.name())

> I am having trouble finding good examples of doing this though. One recent
> example that we did was where we created a wizard using over 15 ui files.
> How does one load up a series ffof ui files for this sort of thing in PyQt?
> What do you need to do if the ui files need to be nested?

Here's a very simple module, that constitutes a Qt dialog, based on designer:


from PyQt5.QtCore import QUrl
from PyQt5.QtGui import QDesktopServices
from PyQt5.QtWidgets import QDialog

from ui.ui_aboutdialog import Ui_AboutDialog
from __init__ import (
    __author__, __author_email__, __version__, __homepage__, __license__,
)


class AboutDialog(QDialog, Ui_AboutDialog):
    def __init__(self, parent = None):
        super(AboutDialog, self).__init__(parent)
        self.setupUi(self)
        self.aboutText.setText(self.aboutText.text().format(
             __version__, __author__, __author_email__,
             __homepage__, __license__))
        self.aboutText.linkActivated.connect(self.openUrl)

    def openUrl(self, url):
        QDesktopServices.openUrl(QUrl(url))


As you can see, ui modules typically are instantiated by multiple inheritance, 
but you're free to deal with these objects however you like.

In short, it all works _very_ similar to C++, but keeps the fun, that working 
with python is (compared to C++). 

If you're a little careful about bytes and encodings, your scripts might even 
keep being python version agnostic. All the generated stuff is (for PyQt5).

Hope, this get you started.

Cheers,
Pete



More information about the PyQt mailing list