One of computer programmers many fantasies (or at least one of my fantasies) is a cross-platform codebase that is easy to maintain. I want to write an application once and then be able to deploy in on any platform in existence. It should integrate into the destination environment in a native manner, but still be similar enough to the other platforms so that issues can be addressed in a way that doesn’t hinder overall progress.
Part of this dream can be fulfilled by using Python and standard library, it is also possible to do much of it by using IronPython and .Net (through the OpenSource Mono framework). However, in each case, you are forced to make a tradeoff. Python and the standard library are wonderful, if you are willing to accept a speed hit. IronPython and Mono also work well, as long as you avoid using any of the Windows specific libraries. Unfortunately, many of neatest .Net features (like Windows Presentation Foundation) are only available on Windows.
Okay, there is always GTK+ or wxWidgets (the same toolkits used for the Gnome desktop), but GTK+ is written in C and while much of the miscellaneous nastiness has been abstracted away, it is still one of the hardest toolkits to work with. Oh, and the documentation sucks. (When any documentation can be found at all.) Thus, we arrive at the final contender: Qt. Qt has had a long and controversial history involving license issues, passion, and miscellaneous geek pride. In fact, it’s due to Qt and some rather divisive decisions made by its developers that we even have alternative toolkits at all. But, despite that history, Trolltech’s recent decision to release the framework under the permissive LGPL license (rather than the noxious GPL) changes all that. In many ways, Qt might just become the cross-platform toolkit of choice
Of course, it’s a given that Qt is tremendously powerful with rich functionality. Applications that are written in Qt are fully cross-platform and behave the same way wherever they are deployed. It uses native widgets whenever possible and has a sophisticated theming system that allows it to follow platform conventions without significantly modifying the underlying code. More importantly, Qt is backed by a passionate developer community and a well funded corporation (Nokia). It is true, you get what you pay for. Qt is well designed and the documentation is simply fantastic. And unlike GTK+ or wxWidgets, the entire toolkit is written in proper object oriented C++.
I first started experimenting with the Qt framework about two months ago, after deciding to tackle a major add-on to one of my favorite writing programs, LyX. As I have said elsewhere, I am not the world’s most
gifted computer programmer (far from it actually), but Qt actually makes it pretty easy to get yourself up and running in a reasonable amount of time. If I were forced to do most of my GUI programming in Qt and C++, that would be an acceptable solution. However, that is completely unnecessary. You see, Qt has one of the most tightly integrated Python bindings available, through the user of PyQt from Riverbank software. Qt plus Python is, quite simply, programming perfection. So while I did some mild experimenting with the Qt frameworks in C++, I have spent much of the past few months doing the heavy work of learning the framework via the use of Python.
In the process, I have made a great many mistakes and a great many more discoveries. And since the final step in any discovery is to share your findings, I will be doing that through this series of articles. Of more interest to most, however, I will also be introducing the fruits of my labors: a somewhat functional prototype of my outliner add-on for LyX.
Part 0: Thoughts on Licenses, Setup and Installation
But before I bore you over the benefits of abstraction and dynamically typed object oriented programming, I should first dispense with some of the nuances and details of setting up a system to run Qt. This also, unfortunately, must include a brief discussion of licenses. While most general computer users likely do not care, the license under which a given piece of software is released is tremendously important. In the case of a most OpenSource software, it determines what rights the users receive and what the developers retain. In some cases, it may even determine if the developer is able to charge for his or her work.
This is why Trolltech’s decision to switch from the GPL (a popular and very restrictive OpenSource license) to the more permissive LGPL is a very big deal. As alluded to above, it allows for commercial developers to link against the Qt libraries without legal fears. Unfortunately, however, the new licensing is only for the most recent version of the toolkit (version 4.5). In addition, while Qt is available under a more permissive license, PyQt is not. PyQt is only available under the terms of the GPL or a separately purchased commercial license. For people who will use PyQt for in-house work or for end users, the distinction is a purely philosophical matter. But if you are a developer who intends to distribute your code (or attempt to charge for it), it is something that you need to consider carefully. Unless you buy a commercial license, PyQt can only be used for GPL related development. The good news, however, is that it is perfectly possible to utilize a commercial PyQt license in conjunction with the LGPLed Qt. Further, the costs for a commercial PyQt license are quire reasonable, about $400 dollars per developer. While this might sound like a lot, compare it to the more than $5000 that Trolltech charges for a cross-platform commercial Qt license.
Installation, Setup and Configuration
Now that some of the grit has been sorted, let’s get down to the the minutiae of setting up a PyQt development environment. This guide will focus on using the most recent version of the framework (Qt 4.5.1). Since the procedure varies slightly based upon your platform, separate instructions will be provided for Mac OS X, Linux and Windows.
There are four separate pieces of software that you will need to download and install on your system. 1) The Qt Framework, which you can get from the Trolltech website, 2) the Python interpreter and related libraries, 3) SIP, which is used to generate Python bindings to C and C++ code, and 4) PyQt. In general, the installation procedure goes like this:
- Download and install Qt via the package installer for your platform from the Trolltech homepage. In order to avoid manually compiling the libraries, I would recommend downloading the package entitled “Qt SDK: The Complete Development Environment.”
- Install the GNU compiler tools for Windows (MinGW). If you use the complete Qt4 package in step 1, this will happen automatically. (The GCC tools are typically provided as a package for Linux and can be installed from the Mac OS X system CD or by downloading the XCode developer tools)
- If there isn’t a version of Python provided with your OS, download, install and configure Python. For Windows users, be sure to select the 2.5.4 Windows installer. (Both Linux and Mac OS X ship with a Python interpreter and can skip this step).
- Download and manually compile the SIP library from Riverbank Computing. Note: Because we will be using the most recent version of Qt, you will need version 4.8 or greater. As of this writing, SIP 4.8 was still in development, thus get the most recent development snapshot.
- Download, manually compile and install the PyQt source code for your platform. Note: Again, because we are using the most recent version of Qt, you will need to download the development snapshot. Be sure that the version on the bindings is 4.5 or greater.
While the general procedure may look quite intimidating at first glance, it is actually fairly simple. The platform specific guides below explain how to do everything step by step. There is some variability between platforms, so be sure to select the guide for your OS.