Setting up a Qt project in Visual Studio .NET

1) CREATE A NEW PROJECT:
From the menu in Visual Studio, select

New Project -> Visual C++ Projects -> Win32 Project -> specify project name -> Click OK
Screenshot1.gif

Then, Click Application Settings (on the left) -> Console Application -> Finish
Screenshot2.gif


2) MAKE SOME MINOR CODE CHANGES:
Rename _tmain to main and _TCHAR to char in your main.cpp class.  This step is for cross-platform compatibility.

Screenshot3.gif
Optionally, you may get rid of stdafx.

  • From the Solution Explorer, right click on stdafx.cpp in Source and stdafx.h in Header files and click Remove
  • Then remove the #include "stdafx.h" line from the top of main.cpp


  • 3) DISABLE PRECOMPILED HEADERS:
    From the Solution Explorer, right click on the Project_Name and select Properties -> C/C++ -> Precompiled Headers

    Set Create/Use Precompiled Header to Not Using Precompiled Headers

    Screenshot4.gif
    Screenshot5.gif


    4) ADD THE QT PATH FOR INCLUDES DIRECTORIES:
    From the Solution Explorer, right Click on the Project_Name and select Properties -> C/C++ -> General
  • Add the following libraries to Additional Include Directories:

    $(QTDIR)\lib;$(QTDIR)\include;$(QTDIR)\mkspecs\win32-msvc;

    Screenshot4.gif
    Screenshot6.gif


  • 5) ADD MORE QT DEPENDENCIES FOR LINKING:
    From the Solution Explorer, right Click on the Project_Name and select Properties -> Linker -> Input

    Set Additional Dependencies to

    $(QTDIR)\lib\qt-mtedu303.lib
    $(QTDIR)\lib\qtmain.lib
    glu32.lib
    delayimp.lib
    imm32.lib
    winmm.lib


  • Use qt-mtedu303.lib for the academic 3.03 license.  Otherwise, substitute the correct library and version as needed.
  • Include opengl32.lib to use the openGL Qt plug-in.

    Screenshot4.gif
    Screenshot7.gif


  • 6) GENERATE MOC FILES FOR HEADERS USING QT EVENTS (Q_OBJECT):
    Perform this step for all header files that inherit from a QT class - i.e. any class with Q_OBJECT.  The following example uses a ficticious header file called MyHeaderFile.h You must fully qualify where the header file is or the moc utility will not find it.

    In the Solution Explorer, right Click on MyHeaderFile.h and select Properties -> Custom Build Step -> General
    Set Command Line to $(QTDIR)\bin\moc MyHeaderFile.h -o tmp\moc\moc_MyHeaderFile.cpp
    Set Description to Moc'ing MyHeaderFile.h ...
    Set Outputs to tmp\moc\moc_MyHeaderFile.cpp
    Set Additional Dependences $(QTDIR)\bin\moc.exe;
    Replace MyHeaderFile with the actual name of the header file to be Moc'd. 

    Screenshot8.gif
    Screenshot9.gif


    7) ADD THE MOC FILES TO YOUR PROJECT:
    First, build your project by selecting Build -> Build Solution or (F7) to generate moc files (you'll get a bunch of linking errors - don't worry).

    From the Solution Explorer, right click on the Project_Name and select Add -> New Folder
    Name the new folder "Generated Moc Files" (or whatever you want).

    Again, from the Solution Explorer right click on the "Generated Moc Files" folder and select Add -> Add Existing Item

    Lastly, locate the moc file from the directory specified in step 6 (i.e. tmp\moc\moc_MyHeaderFile.cpp) to add it to your project.

    That's it, you should be up and running.  Good Luck.

    Screenshot10.gif
    Screenshot11.gif


    WHY USE VISUAL STUDIO .NET?

       Answer



    Shaun Tonstad and Aaron Galuzzi had fun writing this tutorial and welcome any comments or suggestions.  Also welcomed are emails with "You're hired" in the subject line.