Monday, March 17, 2008

Using VS 2005 and the Open Source version of Qt for Windows

I found myself with some free time the other day and wanted to play with using Qt on Windows as I have been using it for fun on various Linux flavors for years. I even went so far, on Linux, as to write an 'intellisense' like autocompletion class that I put into my own little code editor (because I deal with a lot of APIs both in my day job and my own activities.)

This time though I wanted to use it with Visual Studio 2005 and let VS do the work for me. Some initial attempts at this (I am using the open source windows version which is available as of Qt4.x) failed miserably but I finally managed to get things going after checking a few blogs (most notably Bennet Smith's.)

The version of Qt I am using is Qt 4.3.4 (you should know this because qmake command line args change occasinally [as they have between 4.x versions already]) so this should work for you if you have installed and built Qt 4.3.4 on your Windows box and have Visual Studio installed correctly.

The general process, at least for me, is to create a new toolbar in VS and add two buttons to it for triggering "qmake -project" and "qmake -makefile", then to create a Visual C++ Makefile project and edit the nmake parameters of the project. After this you should be able to use both intellisense, debug, and build using your normal build methods without a hitch.

Let's get started!

Step 1 - create a new toolbar to contain your new Qt specific commands

We want to create 2 buttons, both run the 'qmake' command, the first one runs qmake to produce a *.pro (qmake project) file and the second one generates a makefile from that *.pro file.

(1)In VS select ToolsExternal Tools to bring up the external tools menu.

(2)Press the Add button, and fill in the following properties as below:
Title: qmake project
Command: <>qmake.exe
Arguments: -project
Initial Dir: $(ProjectDir)

(3)Press the Add button and fill in the following properties as below:
Title: qmake makefile
Command: <>qmake.exe
Arguments: -makefile -spec win32-msvc2005
Initial Dir: $(ProjectDir)

If you wish, you can also set the checkbox for "Use Output Window" for these buttons (I do in case there's a problem) You may also wish to add buttons to launch Qt Designer and Qt Assistant here as well (as I did.)

Now that you have new external tools configured, let's put them on a new toolbar to help us build Qt projects.

To add a new toolbar to VS go to ToolsCustomize and then select the Toolbars tab on the dialog and press the New button. Put in whatever you want for the toolbar name, because you may ultimately have more than one version of Qt installed I recommend putting something like "Qt 4.3.3" as the title.

To add the buttons we want to this new toolbar choose the Commands tab on the Customize dialog, then find the Tools entry in the categories listed and in that sub-category find the external commands.

You need to figure out which commands are your qmake commands because for some odd reason VS simply gives them numbers. For most people they will be the two last external tool entries by number but for others who have had other external tools installed and then uninstalled (such as myself) you may have to count the number of entries for external tools in the drop down menu on the main UI because in my case my qmake entries were #10 and #11 and I had external tool numbers all the way up to 23.

You can simply drag the entries from the sub category list onto the toolbar and the title should show up and tell you if you've selected the correct entry. If you just see 'external tool #" it's the wrong one.

Step 2 - Create a VS project file for use with Qt

(1)Choose FileNewProject
(2)Under Project Types, choose General
(3)Choose Makefile Project from the list of sub projects now showing
(4)Give it the location you desire
(5)Press the Ok button and when the application wizard pops up press the Finish button

Now that the project has been created, we need to edit some of its properties:

(1)Bring up the project's properties
(2)Choose Debug as the project type
(3)Choose Configuration Properties
(4)Choose NMake, and under the NMake properties on the right side:
(5)Set Build Command Line to: nmake debug-all
(6)Set Rebuild All Command Line to: nmake debug-clean debug-all
(7)Set Clean Command Line to: nmake debug-clean
(8)Set CLR Support to: No CLR support
(9)Add to Preprocessor Definitions: UNICODE; QT_LARGEFILE_SUPPORT; QT_DLL; QT_GUI_LIB; QT_CORE_LIB; QT_THREAD_SUPPORT
(10)Set Include Search Path to the corresponding locations of your Qt install:
(mine for example are: C:\Qt\4.3.4\include\Qt;C:\Qt\4.3.4\include\Qt3Support;C:\Qt\4.3.4\include\QtAssistant;C:\Qt\4.3.4\include\QtCore;C:\Qt\4.3.4\include\QtDBus;C:\Qt\4.3.4\include\QtDesigner;C:\Qt\4.3.4\include\QtGui;C:\Qt\4.3.4\include\QtNetwork;C:\Qt\4.3.4\include\QtOpenGL;C:\Qt\4.3.4\include\QtScript;C:\Qt\4.3.4\include\QtSql;C:\Qt\4.3.4\include\QtSvg;C:\Qt\4.3.4\include\QtTest;C:\Qt\4.3.4\include\QtUiTools;C:\Qt\4.3.4\include\QtXml)
(11)Set the project type to release and perform steps 5-10 replacing the word debug with release

Now, save everything, and before you do anything else, press the qmake project button on your toolbar (this should generate a *.pro file in the solution directory) and then press the qmake makefile button (this should result in a Makefile being placed in the project directory) and now you can simply choose "Build Solution" from the Build menu of VS and everything should be up and running.

Hope this helps someone out there :)

5 comments:

  1. i tried it but at the end, i cliked build the solution, an error occured and it wrote that the " debug-all " is not found
    can u help me

    ReplyDelete
  2. Make sure your Qt generated Makefile contains an entry for debug-all.

    It should look like this (or very similar):

    debug-all: $(MAKEFILE).Debug FORCE
    $(MAKE) -f $(MAKEFILE).Debug all

    ReplyDelete
  3. I have tried to set this up on VS 2008, and when I run nmake, it recursively creates makefiles with another instance of the extension,
    Makefile.msvc
    Makefile.msvc.msvc
    Makefile.msvc.msvc.msvc
    ...
    Any ideas/thoughts? I am using Qt4.4.2, VS 9.0/2008, windows xp

    ReplyDelete
  4. I haven't tried this on VS2008 yet, sorry. I'll post if I get a chance. Weird problem you're having though. I tend to avoid the latest and greatest VS versions until the first service pack where they fix most of the bugs...

    ReplyDelete
  5. It is remarkable, it is rather valuable information

    ReplyDelete