How to Add QT Support to an Existing Visual Studio Project

QT is a cool GUI framework and the Visual Studio add-in is pretty neat… it allows you to code and deploy C++/QT apps without having to deal with QT’s include directories, linker dependencies, environment variables and all that jazz. You simply create a new QT project (an application or library) in Visual Studio just as you would for any regular VC++ project, select the modules you want to use, and thunderbirds are go!

But annoyingly enough, the add-in doesn’t allow you to take an existing vanilla Visual Studio project and turn it into a QT enabled project. Sure you could forget about it and manually do everything the add-in does for you. Or you could create a fresh QT project and migrate your source and settings to it, but that’s a royal pain if your project is complex. Since I’ve seen a few people asking about this missing feature and it’s bothered me in the past, I thought I’d post the solution I figured out (only tested on VS2008)… it’s actually very simple and it doesn’t even involve any assembly level hacking 😉

  1. Open your .vcproj file in your favourite text editor – if you’ve never seen one of these in the wild, it may surprise you to know that it’s plain XML… that’s what allows SVN to merge project changes without disaster.
  2. Change the Keyword part near the top to Keyword=”Qt4VSv1.0″ and adjust for your desired version.
  3. Add this to the globals section:
    <Global Name="lupdateOnBuild" Value="0"/>
    <Global Name="MocDir" Value=".GeneratedFiles$(ConfigurationName)"/>
    <Global Name="MocOptions" Value=""/>
    <Global Name="QtVersion Win32" Value="$(DefaultQtVersion)"/>
    <Global Name="RccDir" Value=".GeneratedFiles"/>
    <Global Name="UicDir" Value=".GeneratedFiles"/>
  4. For each build configuration (i.e. Debug and Release):
    • Add AdditionalIncludeDirectories=”$(QTDIR)include” to the VCCLCompilerTool entry
    • Add AdditionalLibraryDirectories=”$(QTDIR)lib” to the VCLinkerTool entry
  5. It should build now… open the QT settings for the project, select the modules you want (probably Core and GUI at a minimum) and you’re done.

Next step??? Someone write a script to automate this!

About Nathan Pitman

Undergrad software engineer at the University of Auckland
This entry was posted in C++ and tagged , , , , , , , . Bookmark the permalink.

6 Responses to How to Add QT Support to an Existing Visual Studio Project

  1. marielle says:

    Thanks for this. I am migrating projects to VS2010 and it works differently from VS2008. VS2010 has changed to reset the environment variables with every individual project compile so I had to convert libraries projects that just include headers with QT includes to be QT projects because QTDIR wasn’t set anymore. I found that all I need to do is change that keyword to “Qt4VSv1.0” and then load the project into VS2010 and use QT to convert to a QT addin and then switch the QT version. If I’m lucky I didn’t screw anything up 🙂

    • gina says:

      Hello, I use VS2010 to do these steps.
      But I can’t find the global section in .vcxproj file
      (It seems VS2010 is called .vcxproj and the previous VC version called .vcproj )
      Do you know how to do your step 3 in VS2010?

      • gina says:

        Sorry, I misunderstanding…
        There is a .vcproj file in VSO2010, and exist a globals section.
        The steps are works !
        Thank you for your information!!

        • Tim says:

          Gina,

          Where did you find the information to edit in a VS2010 project? I am looking for a .vcproj file and can’t seem to find one.

          -Tim

  2. Pingback: Visual Studio 2013, convert project to QT5 - CSS PHP

  3. Dex says:

    I’m trying this for Visual Studio 2015 with QT version 5.8, but there seems to be a keyword problem after changing the XML. The error is: the attribute “Name” in element is unrecognized. Any help with this, anyone?

Leave a Reply

Your email address will not be published. Required fields are marked *