138

I struggled finding a how-to which provides a stable solution for using Qt with Visual Studio 2010, so after collecting all the bits of information and some trial and error, I would like to write my solution into a guide.

The problem, or why is it not possible to use prebuilt binaries?

It seems that using binaries built for Visual Studio 2008 might work in some special cases, but I found them not to work. In my case they compiled OK, but they produce runtime errors, like this:

problem2

or when started from Visual Studio 2010:

problem

Update: I found a blog post analysing why does it work for some people, while it does not for others. In one word, it depends on whether you have Visual Studio 2008 installed on the same machine, or not. http://blog.paulnettleship.com/2010/11/11/troubleshooting-visual-studio-2010-and-qt-4-7-integration/

The most important thing (that I stupidly didn’t realize) was the fact that you CANNOT use the Visual Studio 2008 compiled libraries and dll’s (available on the Qt webpage) if you don’t have Visual Studio 2008 installed. The reason is because the Qt SDK you download is a debug build which is dependant on the VC9.0 DebugCRT, meaning it needs the Visual C++ 2008 Debug Runtime installed, which is NOT available as a redistributable installer. The only way to install the DebugCRT is to install the entirety of Visual Studio 2008.

hyperknot
  • 12,019
  • 22
  • 87
  • 143
  • Thanks for your effort. Also, it might be helpful to explain why one shouldn't use the VS 2008 binaries. I did develop some prototype test projects with them and VS2010 without experiencing any issues, but I do recall someone mentioning that there are deployment issues? – Ralf Apr 09 '11 at 07:10
  • I've added my experiences to the last section. Generally I found lot of reports on the net which tell you that you must compile Qt if you'd like to use it with VS2010. There is one page which tells you that it might be the presence of VS2008. – hyperknot Apr 09 '11 at 11:07
  • Fantastic, I'm sure many people will find this useful :) – Ralf Apr 09 '11 at 11:42
  • Hey, [here's what I get](http://pastebin.com/Uv1ggnLJ) when I do `nmake`. I used the following before attempting to build: `configure.exe -release -no-ltcg`. – Mateen Ulhaq Apr 15 '11 at 02:41
  • @muntoo Thats seems to be a special problem, I don't know enough about that to say anything, but I'm sure that the guys over the official Qt Devnet forum will be happy to help you: http://developer.qt.nokia.com/forums/ BTW, always post your specs like OS, VS version and things like that when asking for helping in special issues. And try installing SP1, if you haven't yet done it. – hyperknot Apr 15 '11 at 14:33
  • @zsero Does SP1 work with MSVC++2010 Express? – Mateen Ulhaq Apr 15 '11 at 18:10
  • The tutorial you posted is the same as tutorial for vs2005 and vs2008. Except environment variables. I use such command in command promt to set varibles: `SET QTDIR=C:\Qt\ SET PATH=%QTDIR%\bin;%PATH%` – Dmitriy Apr 16 '11 at 20:56
  • @geotavros SET lasts only as long as the current command line session. You need to use SETX or use the GUI in Windows to make a change permanent (or some other specialised tool, like PathEd). – hyperknot Apr 17 '11 at 02:10
  • 2
    For future reference you might be interested to know the [Qt documentation](http://doc.qt.nokia.com/) site actually has the [installation instructions for compiling from source](http://doc.qt.nokia.com/4.7/install-win.html). – Ephphatha Apr 20 '11 at 08:47
  • Hi guys. I have done all this steps but in qtdemo not all examples are work. How can I fix this problem. The question about the problem is at http://stackoverflow.com/questions/5742041/some-of-qt-examples-are-not-work – Mihran Hovsepyan Apr 21 '11 at 09:21
  • @Mihran It doesn't build the examples where you disabled the features with the configure.exe flags. I answered you in that thread. – hyperknot Apr 21 '11 at 14:36
  • This is only valid for debug versions of the programs I guess. – delete_this_account Apr 21 '11 at 15:24
  • Made it into a Q & A and relocated some of the sections. – hyperknot Apr 26 '11 at 13:59

3 Answers3

142

First of all, it’s very important to understand that for using Qt with Visual Studio 2010, it's not possible to use the pre-built binaries which were made for Visual Studio 2008, but you have to compile it from source.

Downloading Qt

On https://www.qt.io/download/

Update 2017: the latest Qt 4.x branch (Qt 4.8.6) has 2 pre-built packages, which are now in the archive section:

  1. http://download.qt.io/archive/qt/4.8/4.8.6/qt-opensource-windows-x86-vs2010-4.8.6.exe
  2. http://download.qt.io/archive/qt/4.8/4.8.6/qt-opensource-windows-x86-vs2008-4.8.6.exe

You should not download Qt by clicking "Qt libraries 4.8.6 for Windows (Visual Studio 2008, 218 MB)", but by clicking on the "zip" link above it.

link-selection

On that link, you get a big zip file like "qt-everywhere-opensource-src-4.8.6.zip". Unzip this into a folder and make its path something nice and small, for example "E:\Qt"

Visual Studio Command Prompt

Now that we have the sources, we need to build the binaries. To do it, open the Microsoft Visual Studio 2010\Visual Studio Tools\Visual Studio Command Prompt (2010) link from your start menu, or even pin it to the taskbar (a good idea). This is a special command prompt which has all the variables set for building with Visual Studio 2010 tools.

Once within the command prompt, navigate to your extracted Qt folder using old-school DOS way, which means you have to change drive letter by E:, enter directories by cd Qt and list dir contents by dir. You can use the tab key for helping you with the directory names. When you have arrived at the correct directory, a dir command should return something like this.

command line

Building Qt

Now it’s time for configure and build. For configuring a minimalist Qt, I'm using the following flags with configure.exe. Just copy and paste it into the command line. Look in the Qt reference manual for what flag to use or not to use.

configure.exe -release -no-webkit -no-phonon -no-phonon-backend -no-script -no-scripttools -no-qt3support -no-multimedia -no-ltcg

Once configure.exe has finished (it was 10 minutes for me), you'll need to start the build process. It will take about 20-30 minutes with the above flags. To start it, just type:

nmake

Setting environment variables

Basically, we are done. All you need to do is to set your environment variables (QTDIR and PATH), which tell programs where to find Qt. If you are on Windows 7, you can use the following command to set QTDIR to your installation dir.

setx QTDIR e:\Qt

For setting the PATH, I strongly recommend using Path Editor. Within Path Editor

add the directory of Qt\bin to your PATH

(it doesn't matter if it's in system path or user path)

If you prefer to use Control Panel\System\Environment Variables, then you can set these there, too.

Qt Visual Studio Add-in

Here you go, after a logoff-logon or a restart, all the Qt demo applications should start correctly (I recommend have a look at bin\qtdemo.exe). Now you can download and install the Visual Studio Add-in (qt-vs-addin-1.1.9.exe) from the Qt download page, it will work perfectly.

addin

Appendix A: Official Instructions:

There is a page at the official wiki at the Qt website called Qt 4.8 Installing Qt for Windows, but I found it lacking important information.

References

Qt DevNet forums
Recommended flags for a minimalistic Qt build
Building Qt 4.5 with Visual C++ 2010
How to compile Qt as static
Qt 4.8: Configure options for Qt
Edit the PATH environment variable in
Windows without pain - op111.net

Community
  • 1
  • 1
hyperknot
  • 12,019
  • 22
  • 87
  • 143
  • 2
    This is great, but I would add -qt-sql-sqlite to the configure flags if you don't want to have problems with your custom-built Qt Assistant. Seems like it needs this plugin to read the documentation databases successfully. – neuviemeporte Jun 11 '11 at 23:24
  • Great guide! One note - I'd being trying to get set up for a day or two and in the process had added some path variables to mingw - these broke nmake and needed to be removed before things worked. – David Hall Jul 27 '11 at 16:51
  • 2
    One more note: VS tools reject building QT if there are some spaces in the path to the folder where QT sources are located. At least it does in my case. – Roman Kruglov Oct 26 '11 at 15:55
  • 10
    Just one question: why doesn't Qt provide a VS 2010 precompiled distribution yet? – Matt Montag Nov 15 '11 at 18:34
  • @rossb83 that is because you need pearl installed to install vs 2010 :) can be found here: http://www.activestate.com/activeperl/downloads – John Snow Mar 23 '12 at 08:24
  • 2
    Just to inform you guys. The link in the original post does not work anymore. It takes quite a while to search the digia site and discover the addin is not even there. The main link one has to follow is: http://qt-project.org/downloads – paul23 Nov 15 '12 at 11:51
  • If you want to build application in debug mode use `-debug-and-release` option instead of `-release`. – cyriel Oct 16 '13 at 11:45
  • Hi! I think you should mention that one might have to install perl and python before. Also, could you give some additional information for the "installation dir"? Your command does not work for me, and I am uncertain what it is doing anyway... – user1809923 Apr 08 '14 at 09:30
  • Also, when compiling Qt-5.2.1 I also do not have a Qt/bin folder. Instead there are many of them, e.g. Qt/qtbase/bin - which one do I need to add to my path variable? – user1809923 Apr 08 '14 at 09:44
25

Qt V4.8.0 contains prebuilt binaries for Visual Studio 2010 so you don't need to do this anymore: http://qt.nokia.com/downloads/windows-cpp-vs2010

parsley72
  • 6,932
  • 8
  • 54
  • 78
  • 2
    parsley72 the prebuilt binaries for Visual Studio 2010 are only useful if you plan on working in 32bit debug mode, otherwise the guide posted by zsero is very useful especially if one wishes to change the options concerning architecture. –  Jun 20 '12 at 17:09
  • Absolutely! But the question was asked back before prebuilt libraries were available so I'm letting people know there is now an alternative. – parsley72 Aug 29 '12 at 09:25
1

Refer below link and it might be useful 1)https://www.slicer.org/slicerWiki/index.php/Documentation/Nightly/Developers/Build_Instructions/Prerequisites/Qt#Windows_3 2)http://eecs.vanderbilt.edu/research/hmtl/wp/index.php/qt-vs/

  • A link to a potential solution is always welcome, but please [add context around the link](http://meta.stackoverflow.com/a/8259/169503) so your fellow users will have some idea what it is and why it’s there. Always quote the most relevant part of an important link, in case the target site is unreachable or goes permanently offline. Take into account that being barely more than a link to an external site is a possible reason as to [Why and how are some answers deleted](http://stackoverflow.com/help/deleted-answers). – Maximilian Ast Aug 24 '16 at 06:46