On Windows, we first need to install the pre-compiled Qt binary package from the Trolltech downloads page. Select the LGPL/Free tab and choose the “Qt SDK: Complete Development Environment” for Windows. It is a fairly hefty package (167 megabytes) and may take a while to transfer. The installer includes the core Qt libraries, several important C++ development tools, and an integrated development environment (IDE) called Qt creator. Luckily, the same installer will work on Windows XP, Windows Vista and Windows 7.
Qt and MinGW
When you run the installer, it will ask for the installation destination. To avoid future problems, this folder should not include any spaces. The default is “C:\Qt\2009.02”, but because I like to keep the root directory of my drive as clean as possible, I changed this to “C:\Qt\Windows”. To begin the installation, press “Install.”
The installer will also download and configure the GNU compiler toolset for Windows (also known as MinGW). By default, MinGW will be installed in a subdirectory under the main Qt directory. In this example, it was installed to “C:\WINDOWS\Qt\mingw”. Note this file path, it will be important to the steps below.
After you finish with the Qt framework, the next step is to download and install Python. You can get the Python interpreter and other associated files from the Python release download page. While the newest version is Python 3.0.1, I would recommend that you download and install Python 2.5.4 instead. When trying to set up SIP with Python 2.6.2 and Python 3.0.1, I experienced a number of errors. These were resolved by using the x86 version of Python 2.5.
When you first run the installer, it will ask whether you want to install Python for all users or for just the current users. Make sure that “install for all users” is selected. Like Qt, you should install Python to a directory without spaces. The default install location will be “C:\Python25”, though I typically change it to “C:\Windows\Python25” so that it matches the installation directory for Qt.
After the Python installer has finished running, you will need to modify one of the Windows environment variables. On Windows XP, the easiest way to access the environment variables to right click on “My Computer” and select “Properties” from the drop own menu. When the “System Properties” window launches click on the “Advanced” tab and then press the “Environment Variables” button. (If you are using Windows Vista or the Windows 7 beta, the easiest way to access the Environment Variables is to go to Control Panel and Search for “Path” in the search bar. All other steps remain the same.)
From the “System Variables” list, select the Path Variable and then press “Edit”.
In the variable value box, type in the path to the Python folder you just installed, the path to the “bin” folder of MinGW (which should be under the MinGW folder from above), and last the path to a Qt program called qmake (which is located in the qt\bin folder of the Qt installation) . You should separate these values from the other paths (and from each other) by a semi-colon. If you have followed all of the instructions in this tutorial, you will add:
And your path variable should now look something like:
%SystemRoot%\System32; C:\Windows\Qt\mingw\bin; C:\Windows\Python25; C:\Windows\Qt\qt\bin
Press “Ok” to close the dialog box and then “Ok” again to close first the Environment Variables and then the “System Properties Pane.” Now that you have changed the Path variable, you need to restart your computer so that the changes can take effect.
SIP and PyQt
Prior to compiling PyQt, you will first need to compile and install SIP, which can be found at Riverbank Software. Though the stable version of the software is currently (4.7.9), you will need to download the latest Windows source code snapshot of version 4.8 (Qt 4.5 doesn’t work with earlier versions of SIP). (Update: Alternatively, you can also find a copy of the 4.8 snapshot here.) You will need the most recent source code snapshot of PyQt 4.5, which is also available from Riverbank. (Update: Multiple people seem to have had problems installing the 4.5 snapshot from the Riverbank website. As a matter of convenience, you can find the April 30, 2009 version here. This is the same version that was used in the writing of this article.)
After you have finished downloading the source files, extract them to the Q
t installation directory (C:\Windows\Qt in this tutorial). This might also be a good time to rename the directories to something shorter than the default snapshot names. I have changed mine to PyQt4 and sip-4.8.
The rest of the steps will be run from the command line prompt (cmd.exe) and must be run with administrator privileges to work properly. Start by going to the start menu and then choosing the run command; then type “cmd.exe” and press enter. (If you are on Windows Vista, you can open the command prompt by typing “cmd.exe” in the search dialog of the start menu. To run with administrator privileges, right click on the top program choice and select “Run as administrator” from the context menu.)
At the command line, first, go to the Qt installation directory by typing:
Then go to the SIP directory:
Prior to compiling, we need to create a proper configuration file:
python configure.py –p win32-g++
After the configuration is done, compile the new file by typing:
This will take some time. After the files have finished compiling, you can install them by typing:
The process is repeated for PyQt. Change to the PyQt4 source directory by typing:
Then configure the make files, compile and install by typing:
python configure.py –p win32-g++
Since Qt is a rather large framework, it may take between 15 and 30 minutes to fully compile.
Update: If you only want to run PyQt programs, the installation process can be greatly simplified by using the automated PyQt installer for Windows. The installer will automatically install a copy of the Qt framework and the PyQt bindings. You will need to install Python separately, however.