Wednesday, April 29, 2009

Qt Part 3 - Configuration for Visual Studio 2008

Before the Nokia buyout Qt could only be integrated with Visual Studio if you purchased the Commercial License. Opensource users were limited to the MinGW toolset. That has now changed with the availability of the Visual Studio Add-In. This is a kind of cut-down version of the full integration that is still available in the Commercial edition, however from what I can see it does offer most of what you need, namely:
  • API Help integration with MSDN (so F1 context sensitive help works)
  • Wizards for Qt projects and skeleton apps
  • Qt Designer integration for forms and layout (although this runs as a separate app)
  • Automated build process (so it runs the Qt pre-processors then compiles and links the app)
Partner this with Whole Tomato Visual Assist X and you have a pretty nice Qt dev environment.
But before you can actually use Visual Studio you need to compile Qt from source so that you get the libs and DLLs that are compatible with VS. The best way to do this is to download the source Framework (not the prebuilt SDK). I think the only difference is that the framework does not contain Qt Creator. These steps assume you already have Visual Studio 2008 installed (any version except Express, which doesn't support add-ins), and that Qt is not already installed.
  1. Download and run qt-win-opensource-4.5.1-mingw.exe (the version number may have changed).
  2. Choose a destination folder, I chose D:\Qt\4.5.1
  3. Select the checkbox to download and install MinGW - it is needed for the build process.
  4. The actual archive unpacking can take quite a while, up to 20 mins or so.
  5. Add Qt to your system PATH - this means the bin folder, so D:\Qt\4.5.1\bin in my case.
  6. Add the system environment variable QTDIR=D:\Qt\4.5.1 (substitute your path)
  7. Reboot your computer for the changes to take effect.
  8. Run the Visual Studio 2008 Command Prompt (or open an ordinary cmd.exe and run vsvars32.bat from Microsoft Visual Studio 9.0\Common7\Tools folder). This will set the environment variables for the Microsoft compiler tools.
  9. cd to D:\Qt\4.5.1 (not the bin folder).
  10. Run the command: configure -platform win32-msvc2008
  11. Answer O for Opensource, and agree to the license.
  12. It will make a new qmake.exe, then build pro files for each library and app.
  13. Run nmake. This can take upwards of 5 hours on a P4 3GHz machine to give you some idea. Also, it will require around 10GB of temporary scratch space to build.
  14. Wait for hours for the compile to finish...it will build debug and release versions of the libraries and DLLs in the \lib folder, and also all the tools and examples.
  15. You can delete the *.obj and *.pdb files to claim back around 9GB of disk space. The easiest way to do this is to open a Command Prompt and cd to the Qt folder. Then enter del /s /q *.obj *.pdb
  16. That's it! If you're a completist you can now uninstall MinGW and also delete mingwm10.dll from the Qt \bin folder, as they are not needed. Confirm everything worked by running qtdemo.exe from \bin.
Now you need to download and install the Visual Studio Add-In. Just double-click the .exe and follow the wizard.
  1. Start Visual Studio 2008.
  2. Click Help -> Contents. This will start MSDN and perform an update of the help files to merge the Qt documentation. This can take quite a while. As in a big cup of coffee.
  3. In VS select Qt -> Qt Options. Click Add and browse to the installation folder (eg D:\Qt\4.5.1). Enter a version name, eg Qt-4.5.1. Click Ok.
All done! You now have an excellent VS 2008 dev environment for creating Qt apps. When you click File -> New -> Project you'll see a new entry for Qt4 Projects, along with some predefined templates. If you use Whole Tomato Visual Assist X (like I do, it's a necessity for any serious VS work) read this blog link on their site which describes how to configure Visual Assist to parse the Qt headers.

The screenshot above shows a Qt app being developed in VS. Notice the Qt menu and the test2.ui form file in Solution Explorer. Double-clicking the .ui file will open Qt Designer as an external app. Here you can create your GUI, then just save, head back to VS and compile. Job's a good 'un!

9 comments:

  1. i have a problem when i type the nmake command it says it needs a src/winmake && nmake -f makefile and some other stuff...what to do???:S please help thanks

    ReplyDelete
  2. Can you give me more detail? What OS are you using and what version of Visual Studio? Did you run vsvars32.bat to set the VS env variables?

    ReplyDelete
  3. Hi Martin, that post was exactly what I was looking for; you've made my day! Thanks.

    Just a note : the nmake needed about 5h on my computer (pentium 4 3GHz with 2Gb of RAM) to complete (the precision may help others) and more importantly it needed about 10Gb of disk space (loads of temp files for the compilation) that nearly crashed my system :)

    ReplyDelete
  4. Good point I'll update the info. Glad it helped you out!

    ReplyDelete
  5. Very good post. Thanks.

    ReplyDelete
  6. Have you tried compiling Qt with VS2010beta1?

    ReplyDelete
  7. With how many files have you ended after compilation? I have 41314 compared to 26484 after installing mingw version. Don't we need some cleanup after building?

    ReplyDelete
  8. Yes you can safely delete all the *.obj and *.pdb files which will save you about 9GB. I didn't bother deleting them because the space wasn't an issue for me.

    ReplyDelete
  9. i have installed qt 2010.04
    i get stuck in step 10 when trying to run command: "configure -platform win32-msvc2008"

    "configure" is not recognized as a command

    ReplyDelete