3 Kasım 2013 Pazar

PyQt via QtDesigner

In this post, we will use a Widget created with QtDesigner, transform it to python code and run it with python interpreter in any machine you want. This is really useful for GUI programming in Raspberry pi. Because Raspberry pi comes with a good python support.
My environment:
  • Ubuntu 12.04 LTS
  • Python 2.7.3 (comes with Ubuntu default)

In order to use PyQt with QtDesigner, we have to install required packages:
$ sudo apt-get install python-qt4 qt4-dev-tools python-qt4-dev pyqt4-dev-tools qt4-designer
After the installation open a terminal and write:
$ python
>>>import sys
>>>from PyQt4 import QtGui
If you do not get any errors, that means you sucessfully installed PyQt. With the installation of "pyqt4-dev-tools", pyuic4 is also installed which is able to transform an XML formatted Qt ui file to python code. To check it, write pyu and press tab in the terminal to see the name completely.
I will not explain how to create a custom Widget in Qt designer here, however I'm gonna give .ui file created via QtDesigner here. You can open it via QtDesigner and play on it.
Assume that we have a hello.ui file, open the terminal and write:
$ pyuic4 -x hello.ui -o hello.py
Now we have hello.py to run in python. Note that "-x" parameter is for transforming the xml ui code to fully python code.
Again in the terminal write:
$ python hello.py
Tata! You will see a "Hello World" label in window and change it by writting something to see how it works. Yes, your GUI design in Qt is working as a python code. I love coding in C++, however this will simplify my job while dealing with my Raspberry pi. The hello.ui code is given below: