Python and Qt

Qt is a set of tools that can be used to create a graphical user interface for your application while there are others available I found Qt to be the best that I have used although you may prefer one of the others available. One of the things that make Qt so good is that it is cross-platform which means that it can be used on several different operating systems even Linux. Another handy feature is that it comes with an application called qt designer this makes creating an interface for your application very easy while there are some things that you will need to implement through code. Qt was created for use with c++ but there is a wrapper available that works with python called pyqt while it does have all the features of Qt there is an extra step that you need to do to change the .ui files that the designer creates into .py files to be used with python I will explain how to do this later. Follow this link for more information about pyqt

There are several ways to get pyqt on most Linux distributions it should be available through the repository but for windows you will have to download both sip and pyqt the links can be found here the previous link also has the files for Mac OS X and Linux if the repository do not contain them

Now as for learning how to use pyqt I would recommend that you get a good python and qt book while there are websites that have tutorials for learning pyqt the are very basic and if you are a beginner you may have trouble with them, but these can be found with a quick google search if you want to give them a try. Another good source of information is the class reference

As for what was mentioned above with the converting files this can be done easily enough. As I use Linux for all my software development that is the method I will use here I expect it will be similar for windows and Mac OS X and a quick google search should help you locate these methods. There is also an IDE called Eric that has the ability to do this for you but if you are a beginner and want the auto-complete feature that most IDEs have I would use something like eclipse with the pydev or aptana plugin, or if you are willing to spend some money there is Pycharm which is an excellent IDE for writing Python code but is intended for Django applications, there is also a community version available with reduced features which I currently use as my IDE of choice.

From Qt Designer to mainwindow

First you need a .ui file which is created by Qt Designer in this case I will call it mainwindow.ui

once you have this it needs to converted into something we can use with Python

Now open your terminal emulator and enter

cd <file location>

So if it was in a projects folder inside your documents folder it would be

cd ~/Documents/Projects/

Now that we are in the correct folder we can change the file using

pyuic4 <.Ui file> > <output .py file>

Or you could use

pyuic4 -xo <.Ui file> <output .py file>

Using our example of mainwindow.ui it would be

pyuic4 mainwindow.ui > mainwindow.py

Or

pyuic4 -xo mainwindow.ui mainwindow.py

This will produce a python file in the same location as the .ui file that you can use

You must remember that every time the .ui file is changed you must run the command again to get an updated .py file

To learn more about using python and Qt like stated above get a good book or look through some of the basic tutorials on the Internet.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s