oggmonster
oggmonster

Reputation: 4872

Qt 4.7.4 and Microsoft Visual C++ 2010 Express

I've just downloaded and installed the latest Qt SDK (4.7.4) and a very recent version of Microsoft Visual C++ 2010 Express.

I want to be able to build applications using the QtSDK from Visual Studio. How do I go about setting this up? I have found several pages with guides but cannot find one the seems to work (all the paths have changed as some of the guides are old, cannot use Qt Visual Studio Add-In because I have Express etc etc).

I would really appreciate some steps to get this done. I have used Visual Studio 2008 professionally for 1 year, and Qt Creator professionally for 4 months, but I still can;t figure this out!

Thanks

Upvotes: 1

Views: 2199

Answers (1)

Liz
Liz

Reputation: 8958

I don't think there are easy steps to follow, that is why the add-in exists, to do all the painful tasks. However, it should not be too difficult, just long and tedious to maintain.

Essentially there are two sets of things to do, setting up your project file, and setting up all the custom build steps for all your files.

Lets start with the project file (vcproj). I'm going to assume that we are going to put all the generated files in a directory called "GeneratedFiles" which is in the directory with the .vcproj file.

When you have your project open the properties page and set the following settings.

Debugging / Environment :

PATH=$(QTDIR)\bin;"$(QTDIR)\bin;$(PATH)

C/C++ / General / Additional Include Directories:

  • .\GeneratedFiles
  • $(QTDIR)\include
  • .\GeneratedFiles\$(ConfigurationName)
  • $(QTDIR)\include\QtCore
  • $(QTDIR)\include\QtGui

    You will need more include paths in here if you are dependent on more than just QtCore, and QtGui, but I just put those too for simplicity.

C/C++ / Preprocessor:

  • QT_LARGEFILE_SUPPORT
  • QT_THREAD_SUPPORT
  • QT_PLUGIN and QDESIGNER_EXPORT_WIDGETS if you want to export widgets to the designer
  • QT_DLL/QT_NODLL depending on whether using shared or static Qt libraries
  • QT_NO_DEBUG/QT_DEBUG one or the other depending on whether using release or debug Qt
  • QT_CORE_LIB you'll need one of these for every dll of Qt that you are dependant on
  • QT_GUI_LIB

Linker / General / Additional Library Dependencies:

add $(QTDIR)\lib to any list you already have.

Linker/ Input / Additional Dependencies:

Add whatever Qt libs you need.

  • QtCore.lib
  • QtGui.lib

And that's pretty much it for the project file. Most of that is largely dependant on what parts of Qt you are using.

Now, on to the hard part. Configuring all your files to build properly. The first thing to do is to set up filters to organize your project. In addition to the normal include and source filters, you will need the following:

  • Form Files - this is where all the .ui files should be
  • Generated Files - this contains 2 more filters Debug and Release
  • Resource Files - this is where any .qrc and images files should be.

Now start adding your files to the project. You won't have any generated files to add at this point, just your normal source, include, ui and qrc files.

Then add your Custom Build steps. The files that need custom build steps are all your ui and qrc files, and all your .h files that contain the Q_OBJECT macro. For each one of these open the property page and select "Custom Build Step / General".

For UI files:

  • Command Line: "$(QTDIR)\bin\uic.exe" -o ".\GeneratedFiles\ui_$(InputName).h" "$(InputPath)"
  • Description: Uic'ing $(InputFileName)...
  • Outputs: ".\GeneratedFiles\ui_$(InputName).h"
  • Additional Dependencies: $(QTDIR)\bin\uic.exe

For qrc files:

  • Command Line: "$(QTDIR)\bin\rcc.exe" -name "$(InputName)" -no-compress "$(InputPath)" -o .\GeneratedFiles\qrc_$(InputName).cpp
  • Description: Rcc'ing $(InputFileName)...
  • Outputs: .\GeneratedFiles\qrc_$(InputName).cpp
  • Additional Dependencies: $(InputPath) and then the list of all the images in your qrc file.

For .h files:

This is the hard one, because there is a different command line for debug, and release, and 32 bit and 64 bit because it is dependant on your project settings. I'll show you one of mine, but the best thing to do is to build one in notepad based on your C/C++ command line.

  • Command Line: "$(QTDIR)\bin\moc.exe" -DWIN32 -DQT_LARGEFILE_SUPPORT -DQT_THREAD_SUPPORT -DQT_PLUGIN -DQDESIGNER_EXPORT_WIDGETS -DQT_CORE_LIB -DQT_GUI_LIB -DWIN64 -D_DEBUG -D_VC80_UPGRADE=0x0710 -D_CRT_SECURE_NO_DEPRECATE -D_CRT_NONSTDC_NO_WARNINGS -D_WINDLL "-I.\GeneratedFiles" "-I$(QTDIR)\include" "-I.\GeneratedFiles\$(ConfigurationName)." "-I$(QTDIR)\include\qtmain" "-I$(QTDIR)\include\QtDesigner" "-I$(QTDIR)\include\QtCore" "-I$(QTDIR)\include\QtGui" "$(InputPath)" -o ".\GeneratedFiles\$(ConfigurationName)\moc_$(InputName).cpp"
  • Description: Moc'ing $(InputFileName)...
  • Outputs: ".\GeneratedFiles\$(ConfigurationName)\moc_$(InputName).cpp"
  • Additional Dependencies: "$(QTDIR)\bin\moc.exe";$(InputPath)

Once you have set all the Custom Build steps, there is really only one thing left to do, add all the generated files to the project. But, so far they don't exist on disk, so test out your custom build steps and try to build the project. It won't complete, but that's ok. The main thing is that it should generate all the files you need. Keep in mind that the generated moc_*.cpp files need to be generated for debug and release so you will have to do this twice.

Add all qrc_.cpp and ui_.h files directly under your "Generated Files" filter in your project. Then add all the Debug\moc_.cpp files under your "Generated Files\Debug" filter, and your Release\moc_.cpp files under your "Generated Files\Release" filter.

Lastly, everything in your "Generated Files\Debug" filter should be excluded from the release build, and everything in your "Generated Files\Release" filter should be excluded from the debug build.

And I think that's it. I never said this wouldn't be painful. The real challenge is remembering to add the custom build steps and such when creating new files. And heaven forbid you ever need to change any of the project settings after all this is set up.

It almost makes you want to shell out 700$ to buy the professional version just to avoid all this.

Upvotes: 2

Related Questions