Qt Moc Tools For Visual Studio 2010



  • I use Qt for visual tools and I recently migrated from SCons to plain vanilla VS2010 (property sheets, etc) for builds. I spent some time making a QT Moc custom build tool which integrates with VS2010 and will automatically run Moc on header files.

    "VS2010 Build Tools":http://www.sunsidegames.com/QtMocTools_VS2010.zip

    These tools are somewhat customized to the way we use Qt (we don't use Designer or anything). Any header that is to be moc'ed has a corresponding cpp file, for example: MyCustomDialog.h has MyCustomDialog.cpp.

    To use this:

    Unzip the file and place the files in a directory, either where you keep your property sheets or just next to your solution.

    Use a property sheet or a environment variable to set a variable named QT_BIN_PATH to the directory that contains your moc.exe.

    In Visual Studio right click the project that has headers you want to moc and choose "Build Customizations...". Navigate to the directory where you placed the files and choose the QtMocRule.targets file.

    For each header you want to moc right click the header, Select General, then drop down Item Type and choose Qt MOC.

    The moc'ed output file will be in the same directory as your header file, of the format moc_Header.cc (so MyCustomDialog.h will have a moc'ed output called moc_MyCustomDialog.cc".

    In your MyCustomDialog.cpp at the end of the file #include the moc'ed output file (#include "moc_MyCustomDialog.cc").

    The rules file will take care of generating the dependencies for you.

    This is nicer than having to copy and paste around moc build commands.

    This doesn't support .ui files but the scripts that are there should be enough for someone to make a Qt Designer tool.

    I wrote these based on this blog and some heavy experimenting to improve the process and of course use moc instead of ui: http://truth2.wordpress.com/2010/09/04/custom-build-rules-for-qt-under-visual-studio-2010/



  • Hi Joe. Is this helpful for Visual Studio 2010 Express as well?



  • Thanks for this. Seems to work for MSVC 2013 (12.0) too. I also added the following to my project vcxproj file to delete the mocs when doing Clean.

    @
    <Target
    Name="AfterCppClean">
    <CppClean
    FoldersToClean="$(ProjectDir)"
    FilesExcludedFromClean=""
    DoDelete="%(CoreCppClean.DoDelete)"
    FilePatternsToDeleteOnClean="moc_*.cc"

    <Output 
      TaskParameter="DeletedFiles" 
      ItemName="DeletedFilesByCppCleanTask"/>
    

    </CppClean>
    </Target>@



  • Thanks for this. Seems to work for MSVC 2013 (12.0) too. I also added the following to my project vcxproj file to delete the mocs when doing Clean.

    @
    <Target
    Name="AfterCppClean">
    <CppClean
    FoldersToClean="$(ProjectDir)"
    FilesExcludedFromClean=""
    DoDelete="%(CoreCppClean.DoDelete)"
    FilePatternsToDeleteOnClean="moc_*.cc"

    <Output 
      TaskParameter="DeletedFiles" 
      ItemName="DeletedFilesByCppCleanTask"/>
    

    </CppClean>
    </Target>@



  • Hi guys,

    i was doing the same with VS2008, 10, 12 and 13 (lately) - using custom build steps for all .h, .ui and .qrc. ( Can post script if someone needed).

    What bothers me - lately i am cooperating with XCode dev which has problem - when i include "moc_*.cpp" (MOCed file) file into .cpp i get Xcode problem - duplicated symbol.

    When I use original *.h file (original class with Q_OBJECT macro) i am ok on XCode but not on visual studio - Linker has problem (Unresolved external symbol).

    What to do? How to fix it? (btw #define Q_OS_MAC macro is workaround, not solution).



  • @Almer_Cz Could you post scripts what you're used? Tried rework joeriedel solution for MOC to cover also UI's and QRC's but failed somewhere - not so familiar with VS custom tools.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.