Introduction to PyQt: light-weight cross-platform Solution for Qt
Posted by Shantanu Sharma
While i was getting handy with Qt, came the problem of porting the application to the other platforms. For me, the main aim while jumping in Qt application development is to make the application run on embedded boards like mini2440, Raspi etc. and this time the board was Raspberry Pi.
There was some UI based project to be ported to Raspi, which required the cross-compiling of whole QT IDE for the Raspi, and i got something called qtonpi. Which, i felt, would require some tedious long hours, which i didn’t have.
Probably you will get this in my next blog on Qt.
But for now, there was another option called (drums beat…) PyQt. 😛
Easy and light weight PyQt. it worked as magic. Almost every linux distribution has python installed and all you need is a PyQt dev-kit, which basically binds the Qt APIs to the python. It is really a light-weight package which can be easily installed in almost all linux-distros in all architectures.
Lets get started:
Setting the work environment and installations:
The dev-kit can be found in synaptic manager or at terminal by typing:-
$sudo apt-get install pyqt4-dev-tools
thats all 😛
Working with python scripts:
1. Create a script file with .py extension
2. There are two options:
a. Either add #!/usr/bin/python to the first line of the file
Reason: originally, unix did not know any field by its extension, so we have the put this c-bank inside the file to let it know which script it is.
I. Add execution permission to the script by $ chmod +x abc.py
b. Or simply write python before the file name like $python abc.py
Working with pyqt4:
a.Design your GUI form in QtCreator to save time and use pyuic4 to compile the .ui file to .py file
(basically python-ui-compiler generate the corresponding code for python from .ui file)
$pyuic4 -x abc.ui -o abc.py
b.Simply start writing your python script from scratch.
Since Qt contains a lot of classes, These classes have been categorized in several modules to ease their access:
- QtCore-handles non-gui functionality like time, file, directory,threads etc.
- QtGui-handles gui components like windows, widgets,colours, fonts, toolbars etc.
- QtNetwork-consists of classes for network programming
- QtXml-handles the XML files. provides both SAX and DOM APIs.
- QtSvg-contains classes for displaying the contents of SVG(Scalable Vector Graphics is a language for describing two-dimensional graphics and graphical applications in XML.) files.
- QtOpenGL handles 3D and 2D graphics using the OpenGL library.
- QtSql module contains classes for working with databases.
These have to be imported as per the need. Goto next blog for rest of the story.