Contrary to popular belief, you can use Qt4 with Visual Studio to create Open Source software.
These instructions are for Qt 4.1.2.
Contents [hide]
1 Building Qt
1.1 Obtain and install the Qt source
1.2 Download and install the patch allowing Qt to be compiled with Visual Studio compilers
1.3 Open a command prompt in the Qt source directory
1.4 Patching the Qt sources
1.5 Configuring your Qt install (and building qmake)
1.6 Building the Qt library
2 Setting up your environment
3 Testing your install
4 Creating Visual Studio projects
5 Maintaining Visual Studio Projects
6 Additional Info
7 Precompiled packages
[edit]Building Qt
[edit]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.1.2\
C:\Qt\4.1.2-msvc2005\ (<-- if for instance you plan on using Visual Studio 2005)
[edit]Download and install the patch allowing Qt to be compiled with Visual Studio compilers
Download Image:Qtnode-acs-qt4.1.2.zip
Unzip the contents of qtnode-acs-qt4.1.2.zip and place all of the files contained in the qt-node-acs-qt4.1.2 directory into the directory that you have unzipped the Qt sources.
[edit]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 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.
A simple way to ensure your environment is setup properly is to use to see if nmake.exe is in your path:
C:\Qt\4.1.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.
[edit]Patching the Qt sources
If you have installed the patch files from qtnode-acs-qt4.1.2.zip correctly, then you should be able to patch the Qt sources by running:
C:\Qt\4.1.2> installpatch412.batYou should see a lot of action, which is normal, which shows all of the files that are being patched:
C:\Qt\4.1.2> installpatch412.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.batYou are now ready for the next step: configuring your Qt install.
[edit]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 qtnode-acs-qt4.1.2.zip file.
But, if you are not sure about which options you want, run configure.exe with the --help option:
configure.exe --helpA 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
After the first option, you can pass qconfigure.bat any of the configure.exe you had previously noted. For instance you might type:
C:\Qt\4.1.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
You will then be informed that qmake is about to be built. Type 'y' followed by
You will then be informed that the Makefiles will be generated. Type 'y' followed by
Once qconfigure.bat has finished, you will be presented with the following message:
====================================================
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
====================================================[edit]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.1.2> nmakeQt 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)
[edit]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.1.2\bin
C:\Qt\4.1.2-msvc2005\bin
C:\Qt\4.1.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 -v
QMake version: 2.00a
Using Qt version 4.1.2 in C:\Qt\4.1.2\libTo see if QMAKESPEC is set correctly:
C:\> echo %QMAKESPEC%
win32-msvc2005[edit]Testing your install
Please test your install from the command-line first. Follow the Hello World instructions.
[edit]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> qmakeTo create a .vcproj which will be used by Visual Studio, we will type:
prompt> qmake -project -t vcapp -o projectname.pro
prompt> qmakeIn the first command above, projectname.pro is the desired name of your project file. Be sure to include the extension .pro with your project file name. 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 template 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).
[edit]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 .sln will be lost when you open up the newly updated .vcproj
Is this true? can someone confirm this? Is there a better way to deal with both .pros and .slns?
[edit]Additional Info
This set of patches has been altered from the Q../Free project. Original download available at: http://sourceforge.net/project/showfiles.php?group_id=49109
The changes that were made are:
the patch file was altered to work with Qt 4.1.2 (patch was originally for 4.1.0)
qconfigure.bat was altered as follows:
User can stop execution before building qmake
User can stop execution before generating Makefiles
Does not automatically call nmake for you. (This is important because if you specify the -static option, you need to be able to run nmake sub-src)
A check was added for qmake\tmp (when not present this is a problem for some users)
[edit]Precompiled packages
A user (QuantumG) has provided this precompiled package of Qt 4.1.2 for Visual Studio 7.1: http://rtfm.insomnia.org/~qg/qt-4.1.2-msvc7.1.zip WARNING USE AT OWN RISK