Using QTDesigner with PyQT and Python 2.6 Using QTDesigner with PyQT and Python 2.6 python python

Using QTDesigner with PyQT and Python 2.6


I started to write my first PyQT application (PyQT is used only to handle GUI), and it seems, that good toolchain is: QtDesigner to generate .ui s and handle resources and some IDE, that can set QtDesigner to edit those. I use Eclipse, cause it is highly customisable.You can compile your .qrc and .ui by demand by doing somewhat like this at application start, or at setup, or any other time:

os.system("pyrcc4.exe -o ui/images_rc.py ui/images/images.qrc")uic.compileUiDir(appDir + '/ui', True)

and then using generated classes this way:

class MyMainWindow(QtGui.QMainWindow):    def __init__(self, owner):        QtGui.QMainWindow.__init__(self)        # 'Ui_MyMainWindow' is the class, that was generated by uic,         # naming convention is: 'Ui_' plus the name of root widget in designer        self.ui = Ui_MyMainWindow()        self.ui.setupUi(self)

or you can load .ui directly when container inits:

    QtGui.QMainWindow.__init__(self)    self.ui = None    uic.loadUi('MyMainWindowUI.ui', self.ui)    #now you have the instance of Ui_MyMainWindow in self.ui too, as above

note, that I have added UI suffix to .ui file's name, it was done to avoid name intersection , cause name of .py file, generated by uic, is not class name starting with 'Ui_', but just root widget's one.

And another one note: at the end of generated file uic places 'import %.qrc name%_rc' (by default is import images_rc) string, so you must aware this when using pyrcc4.

The whole approach is flexible enough, it takes all dummy ui coding work from you; but you still can do some fine tuning in MyMainWindow.ui attribute, where the instance of Ui_MyMainWindow lays; and it avoids unneeded inheritance. Personally, I make _connectSlots and _initIntefrace methods in MyMainWindow to do some work designer cannot.

Still I have to note that writing interface code by yourself is good approach, cause the code, generated by uic, is UGLY. I prefer to load .ui in place by loadUi() because of this :) And if you have a lot of custom PyQT controls, it is so booooring to bring them into QtDesigner..


What I did was I made my own uic executable that's a wrapper for the pyuic.py script.

You'll need...

  1. To get and install py2exe

  2. Replace PYTHON_PATH in the uic.py code below

  3. Edit the qt.conf file in the site-packages\PyQt4 and set Binaries=Path to folder containing the uic exe once it's made.

  4. Put uic.py and setup.py in the same folder

  5. In the command prompt navigate to to the folder where setup.py and uic.py are then run the following command "python setup.py py2exe".

//----uic.py

#uic.pyimport subprocessimport sysargs = ""for arg in sys.argv:     if sys.argv[0] != arg:        args += arg + " "# Start pyuic.py scriptcommand = 'python %PYTHON_PATH%\\PyQt4\\uic\\pyuic.py '+ argsout = ''child = subprocess.Popen(command, shell=True, stderr=subprocess.PIPE)complete = Falsewhile True:    out = child.stderr.read(1)    if out == '' and child.poll() != None:        break    if out != '':        sys.stdout.write(out)        sys.stdout.flush()

//----- setup.py

#setup.py    from distutils.core import setupimport py2exesetup(windows=[{"script":"uic.py"}], options={"py2exe" : {"includes" : ["sip", "PyQt4.QtCore"]}})


You can run pyuic (or rather pyuic4) from hand, in your console. You can make your GUI using designer with Python, just as you would do it with C++.

By the way: I have written all GUI stuff for my app (10k of code) by hand. I don't really like GUI designers and working with generated code, when you need to tweak something.