Visual Studio Express with Qt code completion

Home        KjellKod Code Page    

This article describes how you can setup your (free IDE) Visual C++ 2005 Express (VC++) to work with Qt, and to get Qt code completion.You can either use the mingw32 compiler, or the nmake compiler that comes with VC++, but the focus of this article is to use only nmake

This article was first published on my blog. Any feedback to me you're more than welcome to post there. Similar articles written by other people can be found under:
Compiling Qt4 on Windows” and
Qt4 with Visual C++ 2005



Prerequisites (1 to 4)

1)Download and install Microsoft Visual C++ Express edition in the installation instructions they mention that you should ALSO install the SDK, don't forget to DO just that. Very Important: Don't forget to follow the instructions on that page, since they will describe how to setup the SDK to work for this version of Visual Studio. NOTE that in these descriptions some files that may need to be deleted will be HIDDEN so using the search or 'un-hide' files might be good.

NOTE: If you update Visual C++ express with SP1, then you must also apply some hotpatches, since bugs are introduced with the SP1. See http://support.microsoft.com/kb/930198 for more information

2)Download Qt (if you haven't done it already) . In most cases you would want to use the  qt-win-opensource-src-4.x.y.zip and NOT the  qt-win-opensource-src-4.x.y-mingw.exe

(Only if needed -first try without the patch: patch for 4.3.x can be found here)

3) Unpack Qt to a location that does NOT contain spaces. I.e. "C:\Program Files" is NOT a good choice. I used
I:\lib\Qt\4.3

4) Set System Environment Variables
QTDIRvariable as a system environment variable
I.e.
My Computer->Advanced->Environment Variables
Variable name: QTDIR
Variable value: I:\lib\Qt\4.3


Similarly make another system environment variable
QMAKESPECto win32-msvc2005
Note: If you want to use for example ming-gw you should do this one differently
win32-msvc2005is ONLY if you want to use the compiler & debugger that comes integrated with Visual Studio 2005 Express C++ (damn, that's a long name)

Add also PATH to be the path to your Qt\bin directory i.e. in this case
I:\lib\Qt\4.3\bin

4)Now test that your system environment variables are working. Open up a prompt and type
echo %QTDIR%
echo%QMAKESPEC%

Both times you should have gotten the corresponding value for that system environment variable.


Qt Installation (5 to 7)

5) You need to patch the vsvars32.batto setup the VC++ compiler correctly. The cript that is found in
"
C:\Program Files\Microsoft Visual Studio 8\Common7\Tools"
Make a copy of it also in case you mess up.


Add to the INCLUDE, LIB, and BIN lines according to following:

PATH Original:
@set PATH=C:\Program Files\Microsoft Visual Studio 8\Common7\IDE;C:\Program Files\Microsoft Visual Studio 8\VC\BIN;C:\Program Files\Microsoft Visual Studio 8\Common7\Tools;C:\Program Files\Microsoft Visual Studio 8\SDK\v2.0\bin;C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727;C:\Program Files\Microsoft Visual Studio 8\VC\VCPackages;%PATH%

PATH Changed to:
@set PATH=C:\Program Files\Microsoft Visual Studio 8\Common7\IDE;C:\Program Files\Microsoft Visual Studio 8\VC\BIN;C:\Program Files\Microsoft Visual Studio 8\Common7\Tools;C:\Program Files\Microsoft Visual Studio 8\SDK\v2.0\bin;C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727;C:\Program Files\Microsoft Visual Studio 8\VC\VCPackages;C:\Program Files\Microsoft Platform SDK for Windows Server 2003 R2\Bin;%PATH%

INCLUDE Original:
@set INCLUDE=C:\Program Files\Microsoft Visual Studio 8\VC\INCLUDE;%INCLUDE%

Include Changed to:
@set INCLUDE=C:\Program Files\Microsoft Visual Studio 8\VC\INCLUDE;C:\Program Files\Microsoft Platform SDK for Windows Server 2003 R2\Include;%INCLUDE%

LIB Original:
@set LIB=C:\Program Files\Microsoft Visual Studio 8\VC\LIB;C:\Program Files\Microsoft Visual Studio 8\SDK\v2.0\lib;%LIB%

LIB Changed to:
@set LIB=C:\Program Files\Microsoft Visual Studio 8\VC\LIB;C:\Program Files\Microsoft Visual Studio 8\SDK\v2.0\lib;C:\Program Files\Microsoft Platform SDK for Windows Server 2003 R2\Lib;%LIB%

Run the
vsvars32.batscript which sets up the system environment for the VC++ compiler nmake.
I.e. you are standing in
%QTDIR%then you are running WITH the " signs:
"
C:\Program Files\Microsoft Visual Studio 8\Common7\Tools\vsvars32.bat"

6 ) You can choose to use the Qt patchfor your Qt version (make sure you are using the right one) - which should enable more compiler support for Qt.
However I recommend you to try it without the patch to start with and only use it if you run into trouble; the trolls update Qt quite frequently and the patch might not be needed (or might not work).

Without patch:make sure you are in %QTDIR%
run
configure
(possibly with any alterations to the options, test it with configure –help)

With patch:make sure you are in %QTDIR%
then run installpatch42.bat

If no error occurs you proceed then to running the qconfigure.batscript as
qconfigure.bat msvc2005
Answer Yes to all questions.

7) Run nmakewhich will compile & install Qt.

Setup VC++ Environment (8a to 8c)

8a)Include files that you need when including Qt into your source code
Go to
Tools->Options->Projects and Solutions->VC++ Directories->Include files
Add the following either with the syntax of $(QTDIR)\... or with the full path:

I.e you should add
$(QTDIR)\include
$(QTDIR)\include\Qt
$(QTDIR)\include\QtCore
$(QTDIR)\include\QtGui
$(QTDIR)\include\QtNetwork
$(QTDIR)\include\QtSvg
$(QTDIR)\include\QtXml
$(QTDIR)\include\Qt3Support
$(QTDIR)\include\ActiveQt

... etc, for all the include directories that you might need


8b)Library files that you need when including/building Qt & source code
Go to
Tools->Options->Projects and Solutions->VC++ Directories->Library files
Add
$(QTDIR)\lib

8c )Source files that you need to enable Intellisense to do Qt code completion
Go to
Tools->Options->Projects and Solutions->VC++ Directories->Source files
Add $(QTDIR)\src

If you had an old project than maybe you also need close down VC++ and then delete the IntelliSense file (*.ncb). Don't worry when you start the project again the file will be re-created.


Create your first project – Qt Hello World)

OK, now you're ready to try your first project. Since Qt already comes with a very easy and nice way to manage the make system we're going to use that (For the lazy ones you get get my example project here).

Create an very simple .pro file (these can also be autogenerated)
This will be the basis for your Qt auto-updated Makefile later
It can look something like this (whatever_project_name_you_like.pro)

TEMPLATE += app
CONFIG += qt warn_on
SOURCES += main.cpp
TARGET = whatever_project_name_you_like
win32:debug:CONFIG += console

Creating a new Project as such:
File->New Project->Visual C++ ->General->Makefile Project
(set up Name, Location etc according to your liking)

Add a file also to the project:
(Right click
project -> Add new item -> C++ file (call it main.cpp)
Make it look something like this:


#include< QString>
#include
< iostream>

int main()
{
QString myString = "Hello World";

std::cout << myString.toLatin1().data()
          << std::endl;
}

 

Now set up your environment to autogenerate Makefiles and compile according to your .pro file
Configuration Properties -> NMake -> General
Build Command Line qmake && nmake debug
Rebuild Command Line qmake && nmake debug-clean
Clean Command Line nmake debug-clean

Output  debug/projectname.exe
(Just an example on the output)

Try compiling it, a Makefile should be generated from the .pro configuration and the whole project should compile. Test it with ctrl+F5

Anything else?

When I tried to compile my first project (completely unrelated, but it was QuickFix) I ran into some trouble due to that the wchar_twas defined in an incompatible way in the old libraries compared to Visual Studio 2005 Express. This could be rectified by setting up the QuickFix project so that the wchar_tshould use the code definition rather than the VC++ definition.
To do this: Right-click on project (or sub-project), choose
References->Configuration Properties->C/C++->Language->
                                                                               Treat wchar_t as a built-in type
Change from Yes to No

Good Luck :-)

Hello world example (zip)





¨