Monday, April 26, 2010

How to: Setup Qt 4.5+ Visual Studio Integration

By dcsoft
QT IS NOW RELEASED UNDER LPGL

Qt is a cross platform GUI toolkit which was acquired from Trolltech by Nokia.  Starting with Qt 4.5, it is dual-licensed under both LPGL and Commercial.  The Commercial license can be a bit pricey, but the LPGL license means you can use it free of charge to develop proprietary, commercial, closed-source software.  Instantly, Qt has become available to a wide audience of software developers (DCSoft included).  Thank you Nokia!

More than a year ago, DCSoft has become very interested in Qt.  Much as we love MFC, it hasn’t changed much since 1999, and this is 2009.  Qt is easy to learn given its concise syntax and documentation, plugs into Visual Studio (more below), comes with a supported toolset including Designer (a resource editor), Linguist (a localization tool), and Qt Creator (an IDE), giving Qt more momentum than MFC, and a higher performance alternative to .NET.  It’s easily one of the best ways to create Windows apps, never mind it can also target Mac, Linux, and several embedded devices.

Starting with Qt 4.6, the LPGL version now comes pre-built for Visual Studio 2008 RTM.  If this fits your need, you can simply install it and skip to INSTALL VISUAL STUDIO ADD-IN. However, you will need to build Qt yourself if:

  1. You are using VS 2008 and you have installed SP1 and/or the ATL Security Update, and you are building your app with the _BIND_TO_CURRENT_VCLIBS_VERSION defined (or another means to specify the non-RTM version).  This is because both Qt and your app need to be built with the same version of the Visual Studio redistributables, and Qt has been built with RTM, but your app is not being built with RTM.
  2. You are using any version of VS 2005.  This is because Qt does not come prebuilt for VS 2005.

Here’s how to get our favorite IDE (Visual Studio 2005/2008) working with Qt!

DOWNLOAD QT SOURCE CODE

As the option to download only the Qt source code is a bit obfuscated on the current Nokia website, please follow these directions:

  1. Go to http://qt.nokia.com/downloads and click the Go LGPL button.
  2. Since you want only source code, look at the Qt: Framework Only column on the right.  Click e.g. Download Qt libraries 4.6.1 for Windows (VS 2008, 194 MB) — choose the VS2008 one.
  3. This starts the download for pre-built Qt.  But we don’t want that as we will be building Qt ourselves.  A page will appear with <Source code available on this link>.  Cancel the Save As dialog in your browser to cancel the binaries download you had clicked on, then click on this link, e.g.http://get.qt.nokia.com/qt/source/qt-everywhere-opensource-src-4.6.1.zip
  4. Unzip the file into e.g. c:\qt\4.6.1-vc.  Please use a path with no embedded spaces, as the Qt build tools have issues with them.
SET ENVIRONMENT VARIABLES

Open Computer Properties | Advanced system settings | Environment Variables:

  1. Edit environment variable to add:  QTDIR = c:\qt\4.6.1-vc
  2. Edit the PATH environment variable to add:  %QTDIR%\bin
  3. Either close all command prompts and Visual Studio instances, or reboot the computer so the new command-line takes effect.
BUILD VC++ VERSION OF QT
  1. Open a Microsoft Visual Studio Command Prompt, which is a command console with environment variables set for the specified VS.  This is easily accomplished using Start | All Programs | Microsoft Visual Studio 2005 (or 2008) | Visual Studio Tools | Visual Studio 2005 (or 2008) Command Prompt.
  2. Cleanup any previous build:
    1. c:\> cd c:\qt\4.6.1-vc
    2. c:\qt\4.6.1-vc> nmake distclean
    3. c:\qt\4.6.1-vc> rm -rf tmp*  // <– recursively remove all tmp\ folders and files

This requires finding a *nix workalike.  I use Total Commander to search for Tmp*, select all the found files, and delete them with one keystroke.

3. Run Configure to target platform vc2005 or vc2008:

c:\qt\4.6.1-vc> configure -platform win32-msvc2005<other options as needed>

Substitute win32-msvc2008 for VC2008

Run Configure with no parameters to see a help screen.  Configure generates nmake compatible makefiles to build all the Qt DLL’s, tools, and demos.

4.  Run nmake to build.

c:\qt\4.6.1-vc> nmake

It will take awhile, but this grinds through building the specified Qt DLL’s, tools, and demos with Visual Studio.

INSTALL VISUAL STUDIO ADD-IN

The Qt Visual Studio Add-in is indispensible for developing Qt apps in Visual Studio. The Add-in has replaced the previous Qt Visual Studio Integration, which was only available to Commercial customers.  Now the Add-in is used by both LPGL and Commercial licensees, and the Integration has been deprecated.  While the Add-in does not allow integrated .ui editing (it instead launches Qt Designer to edit .ui files), it is fully supported and maintained by Nokia, whereas the Integration hadn’t been modified since the 4.3/4.4 timeframe.

Because Visual Studio Express does not allow add-ins, using these free versions of Visual Studio is not recommended for Qt development.  You need at least Visual Studio Standard (Pro, Team System, etc. of course will also work).

1.  Download and install the Qt Visual Studio Add-in to install the Qt plug-in into Visual Studio (both 2005 and 2008 are supported by the add-in).

2.  Start Visual Studio.

3.  Select menu Qt | Configure Qt Versions.  Add c:\qt\4.6.1-vc.

4.  Now Qt is fully functional, and you can use VS2005/2008 to build Qt apps.

QT IN VISUAL STUDIO

1.  See Qt menu item.  Launch Qt Designer (the Resource editor) and Qt Linguist (the localization tool).

2.  Create new Qt projects.  File | New project, select Qt4 project.

3.  Read Qt Help.  Available from Help menu (Qt help is merged in with Visual Studio Help and viewed in Document Explorer.)  Or, manually launch

C:\Qt\4.6.1-vc\bin\assistant.exe

4.  The Whole Tomato Visual Assist X plug-in is highly recommended to develop Qt within Visual Studio.  See thisblog entry for tips.

No comments:

Post a Comment