2010-09-12 // Compiling UI and resource files with PyQt
PyQt is a python binding for popular Qt library (which became LGPLed for non-commercial purposes recently). If you use Qt Designer, you have to compile XML description of UI or Qt resource files to Python code to use them in your application.
In C++, you can easily use qmake
tool or Automake for creating suitable Makefile.
For Python, there is no Makefile needed since Python compiles modules as they are loaded into VM.
With PyQt, you can use uic
module for compiling ui files or loading them as a Python class.
Alternatively, you can use pyuic4
and pyrcc4
command line tools in the similar way as appropriate Qt tools (uic
, rcc
). I'm using this way to produce Python code. Both tools have similar usage:
#compile ui file from Qt Designer pyuic4 ui_file.ui -o compiled_ui_file.py #compile resource file (icons, etc..) pyrcc4 resource_file.qrc -o compiled_resource_file.py
Formerly, I had one shell script to compile all my resource files in project at once (because I was too lazy). This solution has two main disadvantages. At first, compilation of all resources takes more time than compiling of modified files only. This varies with amount of resource files your project has. The second one is more annoying to me. Since I also track compiled versions of UI and resource files (for no particular reason yet) with Mercurial, it always creates new changeset because pyuic4
tool adds information about compile time to Python code comments. So I refreshed my Makefile knowledge (and also used some old code snippets) and created following Makefile for compiling UI and resource files separately.
###### EDIT ##################### #Directory with ui and resource files RESOURCE_DIR = src/resources #Directory for compiled resources COMPILED_DIR = src/ui #UI files to compile UI_FILES = confirm.ui main.ui repair.ui settings.ui statistics.ui #Qt resource files to compile RESOURCES = images.qrc #pyuic4 and pyrcc4 binaries PYUIC = pyuic4.bat PYRCC = pyrcc4 ################################# # DO NOT EDIT FOLLOWING COMPILED_UI = $(UI_FILES:%.ui=$(COMPILED_DIR)/ui_%.py) COMPILED_RESOURCES = $(RESOURCES:%.qrc=$(COMPILED_DIR)/%_rc.py) all : resources ui resources : $(COMPILED_RESOURCES) ui : $(COMPILED_UI) $(COMPILED_DIR)/ui_%.py : $(RESOURCE_DIR)/%.ui $(PYUIC) $< -o $@ $(COMPILED_DIR)/%_rc.py : $(RESOURCE_DIR)/%.qrc $(PYRCC) $< -o $@ clean : $(RM) $(COMPILED_UI) $(COMPILED_RESOURCES) $(COMPILED_UI:.py=.pyc) $(COMPILED_RESOURCES:.py=.pyc)
It works with GNU Make (from Cygwin or MigGW on Windows). It compiles files in following manner:
- window.ui –> ui_window.py (GUI definition)
- images.qrc –> images_rc.py (resources)
Now you can easily compile only modified files by only typing
make
in console. Make sure that your current working directory contains Makefile shown above.
Comments

@ben: uic
(pyuic4) is for compiling UI definition (in XML) to C++ or Python source class, respectively. rcc
(pyrcc4) is for compiling so-called resources (mostly images) to a binary blob which has to be compiled into the resulting binary/object file (for C++) or module (Python). UI definition (e.g toolbar button) or some other code can access it using designated methods.

@Martin Plicka: How do you include the .py resource file into your app?

@Fruitful Approach: Just import the module with the compiled resources to your code, then refer to the image like
QPixmap(":/icons/statistics")
UI files from Designer compiled by uic (pyuic4) automatically do this. If you create both UI and resource files by Designer, you just pick the appropriate image resource in the Designer itself.
Leave a comment…
- E-Mail address will not be published.
- Formatting:
//italic// __underlined__
**bold**''preformatted''
- Links:
[[http://example.com]]
[[http://example.com|Link Text]] - Quotation:
> This is a quote. Don't forget the space in front of the text: "> "
- Code:
<code>This is unspecific source code</code>
<code [lang]>This is specifc [lang] code</code>
<code php><?php echo 'example'; ?></code>
Available: html, css, javascript, bash, cpp, … - Lists:
Indent your text by two spaces and use a * for
each unordered list item or a - for ordered ones.
What is the difference between uic vs rcc (or pyuic4 vs pyrcc4)?