Qt4 with Visual Studio
From qtnode
Contrary to popular belief, you can use Qt4 with Visual Studio to create Open Source software. These instructions are for Qt 4.2.3
[NEW] unable to compile any Qt4 app ? see how to fix the bug that appears after installing service pack1 or .NET Framwork 3.0 click here ( trolltech post http://lists.trolltech.com/qt-intere...ad00584-0.html )
Contents[hide] |
Building Qt
Obtain and install the Qt source
- To start, download the source package: Download Qt (Be sure to download the source package, not the package that installs MinGW. The source package is a .zip file, whereas the binary package for use with MinGW is an executable.)
- Unzip your Qt source to a logical location.
Here are some suggestions:
C:\Qt\4.2\
C:\Qt\4.2-msvc2005\ (<-- if for instance you plan on using Visual Studio 2005)
Download and install the patch allowing Qt to be compiled with Visual Studio compilers
- Download acs-4.2.3-patch1.zip from sourceforge.net.
- Unzip the contents of acs-4.2.3-patch1.zip into the directory that you have unzipped the Qt sources.
Open a command prompt in the Qt source directory
Run cmd.exe however you prefer, but you must make sure that the Visual Studio compiler files are in your path.
- If you want to build Qt as a 64-bit library under Windows, all you need to do is use the 64-bit Command Prompt supplied with the Visual Studio Tools.
- If you are using Microsoft Visual Studio 6.0, try running vcvars32.bat from the command-line.
- If you are using Microsoft Visual Studio .NET or 2005, go into the program group called 'Visual Studio Tools' and run the command prompt shortcut.
- Note for Visual C++ 2005 Express users (this may also apply to the non-express version) : make sure you installed the Platform SDK. Step 3 of the Platform SDK installation describes how to add paths to Visual C++. However, you also have to manually add those paths in your environment in order to compile Qt. Here is what you can do before launching the command prompt:
- Add the three paths (LIB, INCLUDE and PATH) to your vsvars32.bat file, normally found under C:\Program Files\Microsoft Visual Studio 8\Common7\Tools. For example:
- Note for Visual C++ 2005 Express users (this may also apply to the non-express version) : make sure you installed the Platform SDK. Step 3 of the Platform SDK installation describes how to add paths to Visual C++. However, you also have to manually add those paths in your environment in order to compile Qt. Here is what you can do before launching the command prompt:
- @set INCLUDE=C:\Program Files\Microsoft Visual Studio 8\VC\INCLUDE;%INCLUDE%
- would become
- @set INCLUDE=C:\Program Files\Microsoft Platform SDK for Windows Server 2003 R2\Include;
- C:\Program Files\Microsoft Visual Studio 8\VC\INCLUDE;%INCLUDE%
C:\Qt\4.2> nmake /?
Microsoft (R) Program Maintenance Utility Version 8.00.50727.42
Copyright (C) Microsoft Corporation. All rights reserved.
Usage: NMAKE @commandfile
NMAKE [options] [/f makefile] [/x stderrfile] [macrodefs] [targets]
Options:
....
Once you are sure that you have the Visual Studio compiler in your path and you're at the command prompt in the Qt source directory, you are ready to proceed.
Patching the Qt sources
If you have installed the patch files from acs-4.2.2-patch1.zip correctly, then you should be able to patch the Qt sources by running:C:\Qt\4.2> installpatch42.batYou should see a lot of action, which is normal, which shows all of the files that are being patched:
C:\Qt\4.2> installpatch42.bat
patching file qconfigure.bat
patching file examples/threads/waitconditions/waitconditions.pro
patching file misc/bcc32pch/Makefile.win32-borland
patching file misc/bcc32pch/bcc32pch.cpp
patching file misc/bcc32pch/bcc32pch.pri
patching file misc/bcc32pch/bcc32pch.pro
patching file mkspecs/win32-bccx/qmake.conf
[snip]
patching file tools/porting/src/rpptreeevaluator.cpp
patching file tools/porting/src/rpptreewalker.cpp
patching file tools/porting/src/src.pro
Press any key to continue . . .
Now run qconfigure.bat
You are now ready for the next step: configuring your Qt install.
Configuring your Qt install (and building qmake)
We will not use the standard configure.exe that ships with Qt. We will use qconfigure.bat, included with the acs-4.2.2-patch1.zip file.
But, if you are not sure about which options you want, run configure.exe with the --help option:configure.exe --help
A list of options will be displayed. Take note of any options that you are interested in. Note that by default, static and debug versions of the library are built - this is usually the appropriate choice for most developers.
Once you have written down (or mentally noted) which options you will use, we will run qconfigure.bat. It will in turn call configure.exe with your chosen options.
qconfigure.bat requires as the first parameter, one of the following compiler choices:
- msvc <- if you are using Visual Studio 6.0
- msvc.net <- if you are using Visual Studio .NET (2003)
- msvc2005 <- if you are using Visual Studio 2005
C:\Qt\4.2> qconfigure.bat msvc.net -release -no-stl
(This command would produce only the release libraries and would exclude STL support in Qt.)
(Note that if you want to build static libraries instead of shared, see Building static)
You will be prompted 3 times while running qconfigure.bat:
- You will be asked to accept the GPL license. Type 'y' followed by
to continue. - You will then be informed that qmake is about to be built. Type 'y' followed by
to continue. This will take a few moments. - You will then be informed that the Makefiles will be generated. Type 'y' followed by
to continue. This will be a short operation.
====================================================
You are now ready to build Qt (msvc2005)
If you specified -static, type 'nmake sub-src'
otherwise, type 'nmake'
To start over, type 'nmake distclean'
and then re-run qconfigure.bat
====================================================
You may run into this bug if you're using Visual Studio 2005 SP1.
Building the Qt library
Once configuration and building qmake are completed, you are now ready to build the library. Simply run nmake:C:\Qt\4.2> nmake
Qt takes a long time to compile. Now is a good time to brew a pot of coffee.
(If you have specified -static as one of your configuration options, see Building static)
Setting up your environment
There are multiple approaches to this, but this is a foolproof way to ensure that your newly create Qt libs are usable.
Prepend the Qt bin directory to your PATH. Depending on where you installed your Qt sources, the full path may be similar to:
- C:\Qt\4.2\bin
- C:\Qt\4.2-msvc2005\bin
- C:\Qt\4.2-visual-studio\bin
Ensure that the QMAKESPEC environment variable is set to the appropriate value.
- If you use Visual Studio 6.0, then win32-msvc is the correct value.
- If you use Visual Studio .NET (2003), then win32-msvc.net is the correct value.
- If you use Visual Studio 2005, then win32-msvc2005 is the correct value.
(Note: this is the recommended practice from qtnode. You can use Qt in other ways with different settings, but this way is foolproof.)
To set your environment variables in Windows, please see http://www.cs.usask.ca/grads/wew036/latex/env.html
Note that you may have to re-open a command window before your settings are changed.
To check that your PATH is set correctly:C:\> qmake -vTo see if QMAKESPEC is set correctly:
QMake version: 2.01a
Using Qt version 4.2 in C:\Qt\4.2\lib
C:\> echo %QMAKESPEC%
win32-msvc2005
Testing your install
Please test your install from the command-line first. Follow the Hello World instructions.
Creating Visual Studio projects
{Note: The following works with MS Visual Studio 2005 and 2003, and has not been tested on other versions - look for an expanded section of this section later}
To create a project, use the -t and -o options.
Normally, (as in the Hello World example), you would simply type:
prompt> qmake -project
prompt> qmake
To create a .vcproj which will be used by Visual Studio, we will type:
prompt> qmake -project -t vcapp -o projectname.pro
In this command, projectname.pro is the desired name of your project file. Be sure to include the extension .pro with your project file name.
From here, you can modify the projectname.pro file if needed. For example, in order to compile an OpenGL project, you should open the file and add QT += opengl at the beginning of the file. You can then continue with:
prompt> qmake
If the above command is executed as written in a directory that includes source files (either in that directory or in a subdirectory of that directory), a new Visual Studio project file is created:
- projectname.vcproj
You can double-click on this vcproj file, or simply type its name in the command window and Visual Studio will create an associated .sln file.
The -t option was used to specify the templates used by qmake. By default, the template is app. The app template creates Makefiles which can be used on the command-line when running nmake. But since we are interest in using the Visual Studio IDE, we want a vcapp which will give us a .vcproj instead of a regular Makefile.
- Note that you can also use the -tp vc option instead of -t vcapp. This will prepend vc to the default template (which is normally app).
Notes on Creating Visual Studio projects
{Note: This may only apply to static builds. If so move this to Qt4_with_Visual_Studio_and_static_C_runtime, or else remove this note}
You can create the Visual Studio project file with your standard .pro file, using: "qmake -t vcapp".
With Qt 4.2.2 "qmake -t vcapp" forgets about the standard libraries like gdi32.lib comdlg32.lib... The best way to fix this is: you generate the Makefiles using "qmake". You look for the LIBS line in Makefile.* and add the lacking libs with "win32:LIBS +=" to the .pro file. This also applies to the method described above.
Maintaining Visual Studio Projects
To add files to your project, you can edit your .pro file and re-run qmake. The only problem is that any customizations to your .vcproj will be lost. However, changes made to the .sln are kept and you can simply reopen the solution file after updating the .vcproj.
Can someone see if there is a better way to deal with both .pros and .slns?
Another way for creating and maintaining Visual Studio Projects
This should be a little better way to deal with Qt and Visual Studio: You only have to maintain a .pro File, and you can use the normal build buttons in Visual Studio, and you get errors and so on shown in VS. This is written for Visual Studio.Net 2003, but should also work with Visual Studio 2005 (may somebody try please? - yes it does)
First you create a new Makefile Project in a empty solution. Then go to the project Property Pages (right click project->Properties). Make sure that Debug is the current Configuration and choose "NMake" in the left menu. Then open the Window for "Build command Line" by clicking the "..." Button. Here you enter:
%QTDIR%\bin\qmake
nmake debug
In "Rebuild Command Line" you enter:
%QTDIR%\bin\qmake
nmake debug-clean
nmake debug
And in "Clean Command Line":
%QTDIR%\bin\qmake
nmake debug-clean
Now the last change, in "Output" enter:
debug\[you app Name].exe
Now do the same thing for release Configuration, but substitute "debug" with "release" ( also in Output Path!)
Now confirm this dialog with OK and create a new File. To do so right click your project and choose Add->Add new Files. Here choose "Text File (.txt)" and as name enter "src.pro", or something similar. In this file enter:
TEMPLATE = app
CONFIG += qt [do you config here, mine would be: "qt warn_on"]
QT += [do you qt-config here]
SOURCES = main.cpp
HEADERS =
TARGET = [your App Name]
win32:debug:CONFIG += console [->if you want to receive qDebug() messages on Windows...]
Save this file and add another file to your project: main.cpp, do it as you did it with the pro File, but choose "C++ File (.cpp)" as file type.
If you want to add another source file to your project, just add it with the add menu, and don't forget to add the files to the right section in your pro-File. This pro file can be used like any normal pro-File, without any problems.
To give you a better start-off here is a little example solution: Image:Visual studio example project 1.zip
Buildproblems with Visual Studio 2005 and service pack 1
After installing service pack 1 for Visual Studio 2005 you will get compiler errors like this one:
c:\qt\4.1.4\include\qtcore\../../src/corelib/tools/qhash.h(805) : error C2244: 'QMultiHash::replace' : unable to match function definition to an existing declaration
c:\qt\4.1.4\include\qtcore\../../src/corelib/tools/qhash.h(790) : see declaration of 'QMultiHash::replace'
definition
'QHash::iterator QMultiHash ::replace(const Key &,const T &)'
existing declarations
'QHash::iterator QMultiHash ::replace(const Key &,const T &)'
This is because of a regression in the compiler and can not be solved by Trolltech.
There are two ways to solve the problem:
- (NEW) you can download the fix by going to [1] and then English>Visual Studio 2005>VS80sp1-KB930859-X86-ENU.exe for more information you can look at the post on the Qt forum [2] (This fix work perfectly !)
- (Not recommended) you can patch the Qt libray
The patch for the QMap sourcecode:
==== //depot/qt/4.2/src/corelib/tools/qmap.h#3 - f:\depot_vs2005\qt-4.2\src\corelib\tools\qmap.h ====
302a303,304
>
> typedef typename QMap::iterator DummyMapIterator;
883,884c885,886
<>::iterator replace(const Key &key, const T &value);
<>::iterator insert(const Key &key, const T &value);
---
> inline typename DummyMapIterator replace(const Key &key, const T &value);
> inline typename DummyMapIterator insert(const Key &key, const T &value);
And the patch for the QHash sourcecode:
==== //depot/qt/4.2/src/corelib/tools/qhash.h#2 - f:\depot_vs2005\qt-4.2\src\corelib\tools\qhash.h ====
386a387,388
> typedef typename QHash::iterator DummyHashIterator;
>
841,842c843,844
<>::iterator replace(const Key &key, const T &value);
<>::iterator insert(const Key &key, const T &value);
---
> inline typename DummyHashIterator replace(const Key &key, const T &value);
> inline typename DummyHashIterator insert(const Key &key, const T &value);
This patch works for Qt 4.2.2 but you'll need these for Qt 4.2.3:
==== //depot/qt/4.2.3/src/corelib/tools/qmap.h#3 - f:\depot_vs2005\qt-4.2.3\src\corelib\tools\qmap.h ====
244a245,246
> typedef typename QMap::iterator DummyMapIterator;
>
893,894c896,897
<>::iterator replace(const Key &key, const T &value);
<>::iterator insert(const Key &key, const T &value);
---
> inline typename DummyMapIterator replace(const Key &key, const T &value);
> inline typename DummyMapIterator insert(const Key &key, const T &value);
and
=== //depot/qt/4.2.3/src/corelib/tools/qhash.h#2 - f:\depot_vs2005\qt-4.2.3\src\corelib\tools\qhash.h ====
329a330,331
> typedef typename QHash::iterator DummyHashIterator;
>
851,852c853,854
<>::iterator replace(const Key &key, const T &value);
<>::iterator insert(const Key &key, const T &value);
---
> inline typename DummyHashIterator replace(const Key &key, const T &value);
> inline typename DummyHashIterator insert(const Key &key, const T &value);
If you're trying to apply this manually, the trick is to put the typedef for the dummy iterator right after the friend declaration for the iterator in QMap or QHash.