[Qt-interest] moc .cpp file in visual studio

Oliver.Knoll at comit.ch Oliver.Knoll at comit.ch
Mon Jun 29 14:14:03 CEST 2009


tank wrote on Monday, June 29, 2009 11:16 AM:

> you can use moc *.h file and will product the moc_*.cpp file.
> when you use the visual studio ,it will automatically use the moc to
> compile the *.h file Linda Maria 写道: 

That is not quite correct: Out of the box Visual Studio does NOTHING automatically, with respect to moc'cing your files!

So either you use the "Qt integration for Visual Studio", which is a plugin for VS and is now freely available from Qt Software website. This does NOT work with the Visual Studio Express versions though! This is a limitation of the Express versions, not the Qt integration: the Express versions don't support plugins! At least the VS 2005 Express does not for sure, don't know about the VS 2008 Express version. But I would be surprised if it would support plugins.

http://www.qtsoftware.com/products/developer-tools
http://doc.trolltech.com/vs-integration-1.4/index.html

Basically the Qt Visual Studio integration realises that a *.h file needs to be mocced if
  * it derives from QObject and
  * has the Q_OBJECT macro in its class declaration

Then it will automatically (upon save of that *.h file) create the necessary post/custom build steps and add the generated moc_xyz.cpp etc. files to your project. It also works similar with *.ui files.

(Note that previously there were separate tools called "Qt Visual Studio AddOn" and "Qt Visual Studio Integration" - not sure if they still exist in parallel. I always got confused, but from what I remember the "AddOn" would simply provide the moc'cing/uic'ing steps and also still worked with Qt 3 (and 4, off course), whereas the "Integration" also embedded the Qt Designer etc. into Visual Studio (and only supported Qt 4)).

Alternativelly you edit the *.h to be mock'ed MANUALLY and type in the necessary POST-BUILD steps (or was it Custom compile steps? Can't remember...). The easiest thing would probably be to create a dummy project with at least one *.ui and *.h to be mocked, and let "qmake -t vcapp" create a Visual Studio project for you. Then see how the *.h files are custom-compiled and copy/adapt that code in your own Visual Studio project. It is a PAIN, but it works (and don't forget to manually add the generated moc_*.cpp etc. files to your project as well, otherwise you'll get linker errors!).

A 3rd, way more convenient solution, would be to use Qt Creator. Or simply use a qmake based project (*.pro files) and create Visual Studio projects from it then and when.


Cheers, Oliver
-- 
Oliver Knoll
Dipl. Informatik-Ing. ETH
COMIT AG - ++41 79 520 95 22



More information about the Qt-interest-old mailing list