Preparations

This tutorial uses the open source version of Qt4 for Microsoft Windows with mingw compiler (gcc port for Windows). We will briefly show installation of Qt and MinGW. However this tutorial is not limited to Windows and can be used on Linux/other platforms supported by Qt as well.

This tutorial introduces no IDE (integrated development environment). The tools used here are just Qt Designer (for creating dialogs), a text editor (for writing code) and of course a compiler suite.

Installation on Windows

In general there are 2 way how to install Qt. Either use installer which install precompiled binaries (recommended on Windows) or to compile Qt from sources.

Installing Qt using installer

On Windows where is no need to compile Qt library from sources as Trolltech provide precompiled binaries with installer which will download and install also MinGW. Go to http://trolltech.com/downloads/opensource/appdev, choose "Qt for Windows: C++" and get qt-win-opensource-4.X.X-mingw.exe. During installation choose also to download and install MinGW. When finished we have to make sure env. variables are set correctly.

Adjusting environment variables

This is how it should look like:

Logout/login or restart after changing environment variables, otherwise it won't work.

Now let's test if we have all we need.

Run Windows commandline go: Start -> Run, type cmd

These commands should work and produce output similar to one shown on screenshot. It some of them produce "is not recognized as an internal or external command" make sure you've correctly done all step above.

gcc -v
make -v
qmake -v

Automated installer installs both debug and release version of Qt. Release version is smaller (QtCore4.dll, ...) and is used for production state of software. Debug version (QtCore4d.dll, ...) is much bigger and is used if you need to debug program during development.

Compiling Qt from sources on Windows

If you decide to compile Qt on Windows from sources, here are is how.

MinGW

Download and install MinGW compiler:

  1. ftp://ftp.trolltech.com/misc/, look for MinGW-3.4.2.exe (local copy)
  2. http://mingw.org

You should have no problem with installation. Installer should adjust your PATH environment variable, so you should be able to run gcc from commandline. If not make also sure you have MINGW\bin (i.e C:\MinGW\bin) added to your PATH. Logout/login again or restart after installation for change of PATH to take place.

Go to MINGW\bin and create make.bat and put following lines in it:

@echo off
mingw32-make %*

This causes you will have to type "make" instead of "mingw32-make" when compiling. Installer created this file automaticly and is places into QTDIR\bin.

Qt source code

Download and unpack lastest qt-win-opensource-src-4.X.X.zip from http://trolltech.com/downloads/opensource/appdev/windows-cpp to C:\qt\4.X.X or other location (path should not contain spaces!).


Compilation of Qt library

When we have Qt sources and compiler ready, we can go to next step - compilation of Qt library itself. Compilation process can take long time (2h+- on AthlonXP 2400+, 512MB RAM). On slow machines with 256MB or less RAM compilation will be misery.

Run Windows command line, go to Qt sources directory and type "configure", then "make".

cd C:\qt\4.X.X
configure
make
If you need to change drive (e.g. you installed Qt in F:\qt\..), go like this:
F:
cd F:\qt\4.X.X
..
..

When compilation is finished, run this command

make clean

It will remove unnecessary files created during compilation and cut down size of whole directory.

Adjusting environment variables

See above how to do this (QTDIR, QMAKESPEC, PATH).

Final words

If we are done with compilation, go to QTDIR\examples and try to run some to see if everything is OK (e.g. examples\mainwindows\menus\release).

On linux there are no debug/release subdirectories. Binary is created directly in the main directory (it at least used to be like this).

As you can see, the directory contains subdirectories "debug" and "release" with one binary present in each. "Debug" binary is considerably bigger than "release" one. It's linked with debug version of Qt library (bin/QtGuid4.dll, QtCored4.dll, note "d" before 4) and contains many symbols and stuff useful for debuging. Release version of binary is linked with release version of Qt (bin/QtGuid.dll, QtCored.dll), and contains no debug stuff. Therefore it's smaller.

By default, Qt is compiled in both, debug and release versions. "configure" command have many switches to tune Qt, but we are happy with the defaults.

Installation on linux

Query your package manager for qt4, it's possible that QT packages are available (for development you would ideally need both "release" and "debug", but as QT compiled in debug mode is quite large, dist. packages usually have onle "release" QT version which is not suitable for debugging). I'll how few tips on compiling QT from sources.

On linux you probably have the compiler already installed, try the command gcc -v. If not found, install it (using package manager of your distribution).

Get lastest qt-x11-opensource-src-4.X.X.tar.gz from http://trolltech.com/downloads/opensource/appdev/linux-x11-cpp and unpack to ie. /usr/local/qt/4.X.X.

cd /usr/local/qt/4.X.X
./configure
make
make clean

Set environment variables in your ~/.bashrc:

export QTDIR=/usr/local/qt/4.X.X
export QMAKESPEC=linux-g++

Project files (.pro)

Before we start programming Qt applications it is essential to know something about project files and Qt build system. In project file (.pro) we define headers, sources, form and other files which out application consists of. You can look at some .pro files in examples directory, later we create our own project file. If we have project file (I assume one .pro file in the directory) we must run program "qmake".

qmake

Qmake is nice utility which comes with Qt distribution and creates Makefile (possibly more than one) for our application (according to what is defined in pro file). Then we can run "make" (part of MinGW distribution), which take Makefile(s) created by qmake and compiles/linkes our application.

We will practice with a small exercise. Run cmd, go to QTDIR\examples\mainwindows\menus and go:

make distclean // this wipes out all files except .pro and source files
qmake // creates Makefile(s)
make // compiles debug and release (normally just debug, copy menus examples elsewhere and see,
// in this case it's because of Qt configuration)
make clean // clean all temporary files (located in tmp, object files, moc generated)
make debug // compiles only debug version
make release // compiles only release version

We should now be ready to start with Qt programming.