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

19 comments ↓

#1 Sujai on 01.18.09 at 12:54 pm

A nice HOWTO. I could point this to some friends who ask me for ‘free C++ compilers in windows’!

#2 Amitava on 01.23.09 at 5:17 pm

Nice documentation. Is there any way to do this in eclipse also?

#3 rajorshi on 01.23.09 at 8:50 pm

@Amitava: Yes, Qt has an excellent plugin for Eclipse as well. Check out http://www.qtsoftware.com/developer/eclipse-integration for detailed instructions.

#4 fernaco on 02.04.09 at 7:15 pm

Excellent article. I bookmarked it, delicious-ed it, and printed it. Thanks.

#5 Amitava on 02.14.09 at 12:57 pm

Many thanks Raj.
PS: Use Yahoo! Search … its better than any other search engines in the world. Come and join to make it better .

#6 ivan on 02.17.09 at 4:39 am

nice tutorial - thanks! using qmake i managed to create a native visual studio tutorial - check the link.

#7 Jan on 02.26.09 at 4:33 pm

Yes, nice, I followed instructions to detail, but it does not work for me in VCExpress2008, as VCE error is: ‘qmake’ is not recognized as an internal or external command,

#8 rajorshi on 02.26.09 at 9:22 pm

@Jan: Are you sure you added $QTDIR\bin to your system PATH?

#9 Jon on 02.28.09 at 1:44 am

Rajorshi,
Thanks for posting the workaround to the mkspecs problem!

#10 Seemanta on 03.03.09 at 1:09 am

Ei, nice post ! I liked it, although I am not too much a fan of MSVC++ myself.

Screenshots were really nice!

~seemanta

#11 dude on 03.04.09 at 12:16 am

excellent article. Thank you! I am loving the new qt 4.5 license, now I don’t worry about that and drop wxwidgets, which I was only using a little bit. Now if I can only convince them to use it at work and drop codegear for guis.

#12 Dule on 03.06.09 at 8:39 pm

Hi,
Nice tutorial , only thing is, I get linker errors, and I can’t find Qt’s *.lib files anywhere. Any suggestions? :)

Regs,
Dushan

#13 Jon on 04.25.09 at 4:00 pm

Dule,

I don’t know what to make of this tutorial/article.

You need to compile Qt 4.5 src to get the .lib library files. VS doesn’t use the .a library files.

I found an article somewhere online that mentions all that, straight from Trolltech’s on wiki, I think. Forgot where it is now.

#14 paul on 06.13.09 at 12:51 pm

when I type qmake -project I keep getting an error message:

Error processing meta file: c:\Qt\4.5.1\lib\qtmain
Error processing meta file: c:\Qt\4.5.1\lib\qtmaind
Error processing meta file: C:\Qt\4.5.1\lib\qtmain

whats wrong?

#15 channa on 10.05.09 at 11:58 am

Excellent tutorial.Thanx a lot.

#16 Dave on 10.19.09 at 2:22 am

It would appear that Qt is not available in binary form pre-built for Visual C++ 2008. Thus compiling from source by hand is the only option for LGPL/VC2008. Is that correct?

#17 Daniel Lewandowski on 10.21.09 at 2:12 pm

Dave, there are QT binaries for Visual C++ 2008. Check this out: http://qt.developpez.com/binaires/en/

#18 Vinay on 11.06.09 at 9:49 am

Thanks for this article. I have a question though. You mention that we should run “configure”. I don’t seem to find this command anywhere. I installed QT for windows using the installer that comes with the download. Is that all the configure you mention is doing ?

#19 rajorshi on 11.15.09 at 6:58 pm

@Vinay - this article is rather old. I’d suggest you use the prebuilt binaries available for Windows from the Qt website

Leave a Comment