How can I generate python code from a QtDesigner file ? I found pyside-uic but I can't find an example for the syntax. I run win7 and pythonxy with spyder.
8 Answers
pyside-uic is more or less identical to pyuic4, as such the man page specifies:
Usage:
pyside-uic [options] <ui-file>
Options:
--version
show program's version number and exit
-h,--help
show this help message and exit
-oFILE,--output=FILE
write generated code to FILE instead of stdout
-x,--execute
generate extra code to test and display the class
-d,--debug
show debug output
-iN,--ident=N
set indent width to N spaces, tab if N is 0 (default: 4)
I usually use it like this:
pyside-uic -o output.py input.ui

- 5,562
- 2
- 22
- 36
-
1I usually use it like this (in windows): `pyside-uic.exe useful_filename.ui > useful_filename_ui.py` Then I can keep track using this simple naming convention. In future I expect to not use this anymore but instead use QUILoader class (see @Sven below)... – Neon22 Dec 05 '12 at 05:05
-
1I prefer creating the UI-code via pyside-uic vs using a loader because **1st**: with the designer and the xml code you never really get how the stuff is actually layed out. So you keep struggling how to dynamically code Qt yourself. **2nd**: compiling to py then pyc happens once until you change the ui not each time you fire your script! **3rd**: you keep off a lot of complexity from your code. It's `import` vs all this loader stuff! I know it's not really a time question anymore but I always feel like wasting a lot when "loading" the xml. – ewerybody Mar 12 '15 at 12:38
Just tried Pyside's QUILoader, works fine:
from PySide import QtGui
from PySide import QtCore
from PySide import QtUiTools
class MyWidget(QtGui.QMainWindow):
def __init__(self, *args):
apply(QtGui.QMainWindow.__init__, (self,) + args)
loader = QtUiTools.QUiLoader()
file = QtCore.QFile("pyside_ui_qtdesigner_form_test.ui")
file.open(QtCore.QFile.ReadOnly)
self.myWidget = loader.load(file, self)
file.close()
self.setCentralWidget(self.myWidget)
if __name__ == '__main__':
import sys
import os
print("Running in " + os.getcwd() + " .\n")
app = QtGui.QApplication(sys.argv)
win = MyWidget()
win.show()
app.connect(app, QtCore.SIGNAL("lastWindowClosed()"),
app, QtCore.SLOT("quit()"))
app.exec_()
I used Eclipse and QTDesigner to create the .ui file (right-click on module, "New -> Other..", choose "Qt Designer -> Qt Designer Form"). No explicit uic call is needed.

- 985
- 1
- 11
- 27
-
1+1 for providing good, complete example. With this 1 thing I was stuck, however, was QFile directs to the package top dir for the file path, not the dir where this code resides. Just for someone who get stuck in the future. – IsaacS Oct 30 '12 at 18:13
-
5The problem is, loader.load returns a object. But in uic, we get a class, then we can subclass the class according to our need. But in loader.load always return a instantiated object so no subclassing, no customizing. Even it cant load a custom widget: Refer to this bug : https://bugreports.qt-project.org/browse/PYSIDE-77 – Sarim Jul 06 '13 at 19:44
import pysideuic
import xml.etree.ElementTree as xml
from cStringIO import StringIO
def loadUiType(uiFile):
"""
Pyside "loadUiType" command like PyQt4 has one, so we have to convert the
ui file to py code in-memory first and then execute it in a special frame
to retrieve the form_class.
"""
parsed = xml.parse(uiFile)
widget_class = parsed.find('widget').get('class')
form_class = parsed.find('class').text
with open(uiFile, 'r') as f:
o = StringIO()
frame = {}
pysideuic.compileUi(f, o, indent=0)
pyc = compile(o.getvalue(), '<string>', 'exec')
exec pyc in frame
# Fetch the base_class and form class based on their type
# in the xml from designer
form_class = frame['Ui_%s'%form_class]
base_class = eval('QtGui.%s'%widget_class)
return form_class, base_class
You can use this way to load the UI and can also get form_class as well as the base class as return type... but if you do not want to convert, otherwise Yes the following is the correct way.
pyside-uic.exe MyWindow.ui -o MyWindow.py

- 18,287
- 11
- 43
- 96
-
from where did you took pysideuic? Because it is not in PySide package. – Romulus Sep 10 '14 at 16:56
pyside-uic.exe MyWindow.ui -o MyWindow.py
is what I've been doing and it's working fine (as far as I know)

- 1,246
- 3
- 20
- 34
QUiLoader class will do the job without making an intermediate file.
http://www.pyside.org/docs/pyside/PySide/QtUiTools/QUiLoader.html

- 590
- 5
- 17
-
2as of Decemeber 2010 pyside devs agree quiloader is not ready. use pyside-uic instead for now. – Neon22 Feb 04 '11 at 10:11
-
2
Using QtUiTools (as suggested in another answer) is currently discouraged by the PySide team.
Read the full story here: https://groups.google.com/forum/?fromgroups=#!topic/pyside/_s1HPe6XTZs

- 931
- 6
- 8
Read the documentation. In this particular case, http://www.riverbankcomputing.co.uk/static/Docs/PyQt4/pyqt4ref.html#pyuic4:
The pyuic4 utility is a command line interface to the uic module. The command has the following syntax:
pyuic4 [options] .ui-file
The full set of command line options is:
-h, --help A help message is written to stdout.
--version The version number is written to stdout.
-i N, --indent=N
The Python code is generated using an indentation of N spaces. If N is 0 then a tab is used. The default is 4.
-o FILE, --output=FILE
The Python code generated is written to the file FILE.
-p, --preview The GUI is created dynamically and displayed. No Python code is generated.
-w, --pyqt3-wrapper
The generated Python code includes a small wrapper that allows the GUI to be used in the same way as it is used in PyQt v3.
-x, --execute The generated Python code includes a small amount of additional code that creates and displays the GUI when it is executes as a standalone application.
--from-imports Resource modules are imported using from . import rather than a simple import.
-
1D'oh! Totally missed the PySide bit. Luckily, PyQt4 did many things right so PySide didn't have to change much :) – Dec 14 '10 at 20:34
Look at C:\Python27\Lib\site-packages\PySide\scripts\uic.py (or wherever you have python installed). If you look at that script you can see the options labelled and described as in the man page (Which I don't know how to view properly on windows. Tips appreciated.) here http://manpages.ubuntu.com/manpages/precise/man1/pyside-uic.1.html
I got confused for a while trying to look at C:\Python27\Lib\site-packages\pysideuic\pyside-uic.1 as I thought that must be the file that is being called. Even trying to view that as a manual page is impossible for me because of all the extra characters. You can't learn syntax by trying to guess at which characters are extra and which ones aren't!
On windows you can automate this with a batch file of course by saving a text file with the above mentioned line(below for reference) with .bat extension like uic_generator.bat.
pyside-uic MyWindow.ui -o MyWindow.py

- 382
- 2
- 13