How to Generate MOC Files for Headers and UI Files for Designs using Q_OBJECT in Visual Studio C++

Finally found a solution to get rid of Qt Creator forever. I really like to use visual studio which is far more convenient and with better features than Qt Creator.
May be I am just addicted to it VS that’s why. Anyways…

/* How to generate Qt Moc files in Visual Studio 2010, 2012, 2013, 2015. (used Qt version is 5.4.1)
* Its very import for Q_OBJECT class, whenever we want to use signals and
* slots, we have to compile each header with moc.exe.
* Following are the steps to setup the moc.exe for each header file that
* has Q_OBJECT. Do this process for each header file of your Qt project.
* 1. Right click on the header file that you want to moc and go to “Properties”.
* 2. go to Item Type => click on drop down menu and select “Custom Build Tool”
*     now click Apply and you will see one more menu “Custom Build Tool”
*     on the left side.
* 3. Click on the left sided Custom Build Tool menu and select “General”.
* 4. Click on the “Command Line” and add
*    D:\Mylibs\Qt\Qt5.4.1\5.4\msvc2010_opengl\bin\moc QtMainWindow.h -o Temp\Moc\moc_QtMainWindow.cpp
*    (path_of_moc exact_header_file_name -o path_of_the_mocing_file_with_the_exact_name_but_with_.cpp_extension.)
*    (Temp\Moc\ are the temporary folders created inside the Solution Project’s folder for mocing files.)
* 5. Click on OutPuts and add
*    Temp\Moc\moc_QtMainWindow.cpp
*    (folder path for .cpp mocing files.)
* 6. Click on Additional Dependencies and add
*    D:\Mylibs\Qt\Qt5.4.1\5.4\msvc2010_opengl\bin\moc.exe;
*    (path of the moc.exe, that should be in Qt bin folder.)
* 7. Click and Apply and OK.
* 8. Build
* 9. Now you will see a generated Temp\Moc\moc_QtMainWindow.cpp moc file in Temp\Moc\ folder.
* 10.Right click on the “Header Files” filter of your Solution Project.
* 11.Add => New Filter
* 12.Rename it “Generated Files”.
* 13.Right click on it and Add => Existing Item…
* 14.Add the generated moc file Temp\Moc\moc_QtMainWindow.cpp.
* 15.Right click on the generated moc file moc_QtMainWindow.cpp => Properties => Select “Yes” in “Excluded From Build”
* 16.Finally, include the generated moc file in your .cpp file.
*    QtMainWindow.cpp => #include “Temp/Moc/moc_QtMainWindow.cpp” just after the #include “QtMainWindow.h”.
*    Example:
*    #include “QtMainWindow.h”
*    #include “Temp/Moc/moc_QtMainWindow.cpp”
* 17.Done.
* 18.Note: Headers files that you are going to moc, should not be in sub-folder. All headers files which contains Q_OBJECT should be in the root folder of your Solution Project.
* 19. Repeat the steps from 1-7 for UI files if needed.
* If you see any errors when you try to build at step#8. Don’t worry, complete all the steps and it should be built successfully.

Expected Errors: If you do not complie Q_OBJECT header with moc.exe

error LNK2001: unresolved external symbol “public: virtual struct QMetaObject const * __thiscall
metaObject(void)const ” (?metaObject@ bla bla bla bla bla bla…………………..

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: