Entries from January 2009 ↓

Using Qt 4.4 opensource with Microsoft Visual C++ Express 2008

Qt from Trolltech is widely acknowledged as one of the best cross-platform GUI toolkits available. However, installing the Qt open source edition on Windows is not as effortless as “sudo apt-get install qt” on Ubuntu or other Linux flavors. It’s not that hard either, and this post shows you how to develop using the freely available Microsoft Visual C++ 2008 Express as our IDE.

1. I’m assuming you have MSVC 2008 Express already installed. If not, download the offline install ISO from here, mount it (using Daemon Tools for example), and launch the installer from the virtual drive.  Next, download the Windows open source version of Qt from here.

2. Now, you can either extract the Qt source package to a folder where you want it to be installed, or you might want to extract it to a temporary location, and install only the final files to your install directory. Doing the latter of course makes more sense. Except that it is NOT recommended for Windows. I have faced quite a few problems (which I will detail further down the line). Bottom line is – if you have no problems sparing about 1G for Qt, then choose the former approach.

Open up the “Visual Studio 2008 Command Prompt” (available in the “Tools” sub-menu in your Visual C++ start menu entry). For the former approach, issue the following command:

configure

If you want a separate install directory (let’s say in D:\Qt-4.4.3), use the ‘prefix’ flag in this manner:

configure -prefix "D:\Qt-4.4.3"

3. Depending on your system, this takes a quite a while. Oh, and if you face an error like this, fear not:

copy qmake.exe P:\qt-win-opensource-src-4.4.3\bin\qmake.exe
        1 file(s) copied.
Creating makefiles in src...
Generating Visual Studio project files...
Could not find mkspecs for your QMAKESPEC(win32-msvc2008)
after trying:
        D:\Qt-4.4.3\mkspecs
Error processing project file:
P:/qt-win-opensource-src-4.4.3/projects.pro
Qmake failed, return code 3

This is the first of a few problems that crop up when you use a custom install location (i.e. the latter approach). Just copy the “mkspecs” folder from your source directory tree over to your install directory and re-run the configure program.

4. Once ‘configure’ completes, run ‘nmake’. This takes a really long time. If you chose to have a separate install location, run ‘nmake install’ once this completes.

5. Another problem of a separate install directory is that the Makefile forgets to copy the MANIFEST files. So, if at this stage you try to start “designer.exe” from your install/bin folder, you may get an error saying that the application failed to start because MSVCP90.dll was not found.

To fix this, copy over all the “.manifest” files from your source “bin” and “lib” directories over to the install folder’s “bin” and “lib” directories. At this point, you should be able to run Qt-Designer, Qt-Assistant etc from your bin directory.

6. Let us set up a couple of environment variables that make life easier for us. To edit environment variables, you need to right click “My Computer > Properties > Advanced > Environment Variables”. Add a new variable QTDIR pointing to your Qt install directory, and edit your PATH to include Qt’s “bin” directory as follows:

Setting QTDIR

Adding to PATH

 

7. Now let’s try to get Qt’s “Hello World” tutorial program running from the command line. Fire up the Visual Studio Command Prompt, and create a file “Hello.cpp” containing the following code in a new directory called “hello”:


#include "QApplication"
#include "QPushButton"

int main(int argc, char *argv[])
{
    QApplication app(argc, argv);
    QPushButton hello("Hello world");
    hello.resize(100, 30);

    hello.show();
    return app.exec();
}

Now, type the following commands in this new folder:

qmake -project
qmake hello.pro
nmake

This should create an executable “hello.exe”, which you should be able to execute to see your first GUI program using Qt-4.4 and MSVC 2008.

7. I would suggest working from the command prompt, but should you wish to use the Visual Studio Express IDE, here’s what you should do.

Fire it up, and go to “Tools > Options > Projects and Solutions > VC++ Directories”. Add “$(QTDIR)\include” to the “Include files”, and “$(QTDIR)\lib” to the “Library files” drop-down lists respectively.

8. Create a new project (“File > New > Project > General > Makefile Project”) named “HelloQt”.

Go to “Project > Properties > Configuration Properties > Nmake” and enter the following in the build command line “qmake -project && qmake && nmake release-all”. Also enter “release\HelloQt.exe” in the “Output” field. (You may enter corresponding debug versions here as well).

Right click “Source Files” in the “Solution Explorer” and create a new file “HelloQt.cpp”. Copy paste the above program into it.

Run your program using “Ctrl+F5″. You should see this:


Sample Qt 4.4 program running inside Microsoft Visual C++ Express 2008

So there you have it. A crash HOWTO on developing Qt-4.4 programs using Visual Studio 2008 express. Feel free do comment on any problems you may have faced.

~Raj

Ring in the new

Ring out the old, ring in the new,
Ring, happy bells, across the snow:
The year is going, let him go;
Ring out the false, ring in the true.

- Tennyson (“Ring Out, Wild Bells”)

May 2009 be the year that you and I have been waiting for. And the year that ushers in peace in India and the world. And the year of the Linux desktop!

Cheers to a new beginning. Sweets on your desk, true Bong style :-)

 sweets

PS: Did you spot the typo in the box above?

~Raj