Qt4 with Visual Studio
The Open Source version of Qt4 for Windows is provided as source code only and it takes a little bit of work to compile it and set up Qt project in Visual Studio. This guide has been verified using MS Visual Studio 2008 and Qt 4.4.1. Note that Qt versions prior to 4.3.2 required a patch to compile. This is no longer the case so those instructions have been removed. Please see previous versions this page if you need those instructions.
First you must download Windows Qt source code package. You can obtain the newest Qt sources by clicking a link on the main page of QtCentre (be sure to choose the zip archived version) or directly from Trolltech site (again, choose the zipped version).
- Unzip the Qt sources to a folder of your choice. Make sure the path doesn't contain spaces (for example use C:\qt-win-opensource-src-4.4.1 rather than something in C:\Documents and Settings\...). From now on we shall assume you have used c:\qt.
- Open the Visual Studio command prompt (Start->Microsoft Visual Studio 2008->Visual Studio Tools->Visual Studio 2008 Command Prompt).
- Change into the directory where you unzipped Qt sources by typing cd c:\qt
Type configure.exe -help into your console and hit enter. You should see a text similar to the following one:
Usage: configure [-buildkey <key>] [-release] [-debug] [-debug-and-release] [-shared] [-static] [-no-fast] [-fast] [-no-exceptions] [-exceptions] [-no-accessibility] [-accessibility] [-no-rtti] [-rtti] [-no-stl] [-stl] [-no-sql-<driver>] [-qt-sql-<driver>] [-plugin-sql-<driver>] [-arch <arch>] [-platform <spec>] [-D <define>] [-I <includepath>] [-L <librarypath>] [-help] [-no-dsp] [-dsp] [-no-vcproj] [-vcproj] [-no-qmake] [-qmake] [-dont-process] [-process] [-no-style-<style>] [-qt-style-<style>] [-redo] [-saveconfig <config>] [-loadconfig <config>] [-no-zlib] [-qt-zlib] [-system-zlib] [-no-gif] [-qt-gif] [-no-libpng] [-qt-libpng] [-system-libpng] [-no-libjpeg] [-qt-libjpeg] [-system-libjpeg] [-no-libmng] [-qt-libmng] [-system-libmng] [-no-qt3support] Installation options: You may use these options to turn on strict plugin loading: -buildkey <key> .... Build the Qt library and plugins using the specified <key>. When the library loads plugins, it will only load those that have a matching <key>. Configure options: The defaults (*) are usually acceptable. A plus (+) denotes a default value that needs to be evaluated. If the evaluation succeeds, the feature is included. Here is a short explanation of each option: -release ........... Compile and link Qt with debugging turned off. * -debug ............. Compile and link Qt with debugging turned on. + -debug-and-release . Compile and link two Qt libraries, with and without debugging turned on. * -shared ............ Create and use shared Qt libraries. -static ............ Create and use static Qt libraries. * -no-fast ........... Configure Qt normally by generating Makefiles for all project files. -fast .............. Configure Qt quickly by generating Makefiles only for library and subdirectory targets. All other Makefiles are created as wrappers which will in turn run qmake -no-exceptions ..... Disable exceptions on platforms that support it. * -exceptions ........ Enable exceptions on platforms that support it. -no-accessibility .. Do not compile Windows Active Accessibilit support. * -accessibility ..... Compile Windows Active Accessibilit support. -no-stl ............ Do not compile STL support. * -stl ............... Compile STL support. -no-sql-<driver> ... Disable SQL <driver> entirely, by default none are turned on. -qt-sql-<driver> ... Enable a SQL <driver> in the Qt Library. -plugin-sql-<driver> Enable SQL <driver> as a plugin to be linked to at run time. Available values for <driver>: mysql psql oci + odbc tds db2 + sqlite sqlite2 ibase (drivers marked with a '+' have been detected as available on this system) -no-qt3support ..... Disables the Qt 3 support functionality. -platform <spec> ... The operating system and compiler you are building on. (default %QMAKESPEC%) See the README file for a list of supported operating systems and compilers. -D <define> ........ Add an explicit define to the preprocessor. -I <includepath> ... Add an explicit include path. -L <librarypath> ... Add an explicit library path. -l <libraryname> ... Add an explicit library name, residing in a librarypath. -help, -h, -? ...... Display this information. Third Party Libraries: -no-zlib ........... Do not compile in ZLIB support. Implies -no-libpng. -qt-zlib ........... Use the zlib bundled with Qt. + -system-zlib ....... Use zlib from the operating system. See http://www.gzip.org/zlib + -no-gif ............ Do not compile the plugin for GIF reading support. -qt-gif ............ Compile the plugin for GIF reading support. See also src/plugins/imageformats/gif/qgifhandler.h -no-libpng ......... Do not compile in PNG support. -qt-libpng ......... Use the libpng bundled with Qt. + -system-libpng ..... Use libpng from the operating system. See http://www.libpng.org/pub/png -no-libmng ......... Do not compile in MNG support. -qt-libmng ......... Use the libmng bundled with Qt. + -system-libmng ..... Use libmng from the operating system. See See http://www.libmng.com -no-libjpeg ........ Do not compile the plugin for JPEG support. -qt-libjpeg ........ Use the libjpeg bundled with Qt. + -system-libjpeg .... Use libjpeg from the operating system. See http://www.ijg.org Qt/Windows only: -no-dsp ............ Do not generate VC++ .dsp files. * -dsp ............... Generate VC++ .dsp files, only if spec "win32-msvc". -no-vcproj ......... Do not generate VC++ .vcproj files. * -vcproj ............ Generate VC++ .vcproj files, only if platform "win32-msvc.net". -no-qmake .......... Do not compile qmake. * -qmake ............. Compile qmake. -dont-process ...... Do not generate Makefiles/Project files. This will override -no-fast if specified. * -process ........... Generate Makefiles/Project files. -no-rtti ........... Do not compile runtime type information. * -rtti .............. Compile runtime type information. -arch <arch> ....... Specify an architecture. Available values for <arch>: * windows boundschecker -no-style-<style> .. Disable <style> entirely. -qt-style-<style> .. Enable <style> in the Qt Library. Available styles: * windows + windowsxp * plastique * cleanlooks * motif * cde -loadconfig <config> Run configure with the parameters from file configure_ <config>.cache. -saveconfig <config> Run configure and save the parameters in file configure_<config>.cache. -redo .............. Run configure with the same parameters as last time.
These are options you may pass to Qt configuration tool to modify the way Qt will be built. Here you can decide if you want to build both the debug and release versions of the framework (-debug-and-release) or only one of them (-debug or -release). Another important decision is -shared versus -static. I suggest using shared unless you know what you're doing (and in that case you can read about building static Qt on Windows).
There is also another important option, which is not mentioned in the above text. It determines where your Qt installation will end up on your system. You enable it by passing a -prefix switch followed by a path (for example -prefix C:\Qt4-shared). If you don't use -prefix the default will be to install to the source path (c:\qt). This is an acceptable default.
-no-webkit may also be worth considering as webkit takes a long time to compile. A complete line you might want to use could be:
configure -prefix C:\Qt4-shared -release -qt-gif
This will proceed to build qmake.exe and generate lots of makefiles. When it is done you can go on to building Qt.
Issues may arise during the configure stage involving the presence of selected includes and libraries. These issues may be a result of the Windows SDK and DirectX SDK not being configured correctly. In order to avert these you may be required to add additional variables to your environmental varables.
To do so right click on My Computer and go Advanced System Settings -> Environmental Variables.
Add the Following Variables:
INCLUDE with the Values 'Path to Microsoft SDK'\Include;'Path to DirectX SDK\Include
LIB with the Values 'Path to DirectX SDK'\Lib;'Path to DirectX SDK\Lib
PATH with the values 'Path to Microsoft SDK'\Bin
Reboot the system for changes to take effect.
The actual building process is quite simple - make sure you are in the top level folder that contains Qt sources and type in nmake. Then you can walk your dog, go jogging, grab a sandwich or watch StarGate (or StarTrek?) on TV as it'll take some time to finish the compilation. If you don't have loads of time you may want to omit building the demos and examples, in that case use nmake libs tools docs translations. You can probably omit the docs, tools and translations too if you are in a hurry. I haven't tested this though.
When you get your prompt back and you don't see any messages about errors, then you have just compiled Qt for MSVC.
Simply run nmake install. If you didn't use -prefix you will get lots of errors about not being able to copy files to themeselves. This doesn't matter. If you installed Qt to a different directory you can now delete, move or archive the source tree as it is no longer needed. If you didn't you can still delete c:\qt\src if you want to free up some space.
Configure Visual Studio
The last step is to configure Visual Studio so that it knows about Qt. Go to Tools->Options->Projects and Solutions->VC++ Directories->"Show directories for: Executable files" and add c:\qt\bin. An alternative to this that will allow you to use qmake on the command prompt is to add it to your path. To do this go to Control Panel->System->Advanced->Environment Variables->System Variables and add the Qt bin directory to the path variable. At this point it is worth adding c:\qt\lib to your path to save you having to copy the Qt DLLs all over the place. If you have matlab installed make sure you put it before matlab's path!
Because qmake itself adds the Qt #include paths on the command line, Visual Studio doesn't know where the Qt headers are and so can't do code completion. To fix this go to Tools->Options->Projects and Solutions->VC++ Directories->"Show directories for: Include files" and add c:\qt\include\QtGui, c:\qt\include\QtCore and any other modules you will be using, e.g. c:\qt\include\QtOpenGL.
A Simple Project
Open Visual Studio 2008 (things will be slightly different in other versions). Create a new project of the type General->Makefile Project.
Use the following settings:
- Build command line: qmake && nmake debug
- Rebuild command line: qmake && nmake debug-clean debug
- Clean command line: nmake debug-clean
- Build command line: qmake && nmake release
- Rebuild command line: qmake && nmake release-clean release
- Clean command line: nmake release-clean
Add some files to the project and write a MyProject.pro file. Make sure your project file is named the same as your project as this determines the name of the executable that is built. Alternatively you can generate one by running qmake -project in your project directory. If you didn't add qmake to your path you have to specify its full location, e.g. c:\qt\bin\qmake -project but you only have to do that once.
Your project should build now. To run it you also have to make one final change. I'm not entirely sure why this doesn't work out-of-the-box but you have to go to Project->Options->Configure Properties->Debugging. Select 'All Configurations' from the Configuration drop-down, and set Command to $(TargetDir)$(ConfigurationName)\$(TargetFileName)
You can now build and run to your hearts content. If you get complaints about missing Qt DLLs remember to add qt\lib to your path or copy the DLLs to the exe directory or c:\windows\system32. You may also have to restart Visual Studio.
If you have a multicore processor and VS 2008 Express or higher you can build programs on all cores.
Visual C++ 2008’s /MP flag tells the compiler to compile files in the same project in parallel. I typically get linear speedups on the compile phase. The link phase is still sequential, but on most projects compilation dominates.
Add these line to the .pro file for release version:
QMAKE_CXXFLAGS_RELEASE += -MP[processMax]
Then rebuild the application.
Wysota 06:51, 2 March 2007 (CET)
chehrlic 10:35, 8 June 2007 (CET)
Sergey B. 04:25, 21 April 2008 (GMT)
Timmmm 11:22, 16 September 2008 (CEST)