Results 1 to 2 of 2

Thread: How to moc in VS 2017

  1. #1
    Join Date
    Nov 2017
    Posts
    1
    Thanks
    1
    Qt products
    Qt5

    Question How to moc in VS 2017

    Hi,

    I am trying to compile a project in VS2017 which uses the QtCore library. So the project is already created and it has been created using NetBeans, not using the Qt IDE.

    This is what I did so far:

    • Downloaded the last Qt release
    • Unpacked everything in C:\Qt
    • In VS properties > VC++ Directories > Include directories - I added C:\Qt\Qt5.9.2\5.9.2\msvc2017_64\include and C:\Qt\Qt5.9.2\5.9.2\msvc2017_64\include\QtCore
    • In VS properties > VC++ Directories > Library directories - I added C:\Qt\Qt5.9.2\5.9.2\msvc2017_64\lib


    As far as I know, I need to "moc" the headers which contains Q_OBJECTs. I read that is possible to automatize this process with Qt VS Tools, but I don't know how.

    Anyone? Thank you

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: How to moc in VS 2017

    It is easiest to start a new project explicitly as a Qt project in Visual Studio. The wizards that the Qt VS tool adds in to VS insert compilation rules into the project files (as well as into common, shared rules files) that cause moc and uic to run at build time. For example, here is the vcxproj entry to run uic on my MainWindow.ui file:

    Qt Code:
    1. <ItemGroup>
    2. <CustomBuild Include="MainWindow.ui">
    3. <AdditionalInputs Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">$(QTDIR)\bin\uic.exe;%(AdditionalInputs)</AdditionalInputs>
    4. <Message Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">Uic%27ing %(Identity)...</Message>
    5. <Outputs Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">.\GeneratedFiles\ui_%(Filename).h;%(Outputs)</Outputs>
    6. <Command Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">"$(QTDIR)\bin\uic.exe" -o ".\GeneratedFiles\ui_%(Filename).h" "%(FullPath)"</Command>
    7. <AdditionalInputs Condition="'$(Configuration)|$(Platform)'=='Release|x64'">$(QTDIR)\bin\uic.exe;%(AdditionalInputs)</AdditionalInputs>
    8. <Message Condition="'$(Configuration)|$(Platform)'=='Release|x64'">Uic%27ing %(Identity)...</Message>
    9. <Outputs Condition="'$(Configuration)|$(Platform)'=='Release|x64'">.\GeneratedFiles\ui_%(Filename).h;%(Outputs)</Outputs>
    10. <Command Condition="'$(Configuration)|$(Platform)'=='Release|x64'">"$(QTDIR)\bin\uic.exe" -o ".\GeneratedFiles\ui_%(Filename).h" "%(FullPath)"</Command>
    11. </CustomBuild>
    12. </ItemGroup>
    To copy to clipboard, switch view to plain text mode 

    and the entry which runs moc on the MainWindow.h header file:

    Qt Code:
    1. <ItemGroup>
    2. <CustomBuild Include="MainWindow.h">
    3. <AdditionalInputs Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">$(QTDIR)\bin\moc.exe;%(FullPath);$(QTDIR)\bin\moc.exe;%(FullPath)</AdditionalInputs>
    4. <Message Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">Moc%27ing MainWindow.h...</Message>
    5. <Outputs Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">.\GeneratedFiles\$(ConfigurationName)\moc_%(Filename).cpp</Outputs>
    6. <Command Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">"$(QTDIR)\bin\moc.exe" "%(FullPath)" -o ".\GeneratedFiles\$(ConfigurationName)\moc_%(Filename).cpp" -DUNICODE -DWIN32 -DWIN64 -DQT_DLL -DQT_CORE_LIB -DQT_GUI_LIB -DQT_SQL_LIB -DQT_OPENGL_LIB -DQT_PRINTSUPPORT_LIB -DQT_SVG_LIB -DQT_WIDGETS_LIB -DQT_XML_LIB "-I.\GeneratedFiles" "-I." "-I$(QTDIR)\include" "-I.\GeneratedFiles\$(ConfigurationName)\." "-I$(QTDIR)\include\QtCore" "-I$(QTDIR)\include\QtGui" "-I$(QTDIR)\include\QtSql" "-I$(QTDIR)\include\QtOpenGL" "-I$(QTDIR)\include\QtPrintSupport" "-I$(QTDIR)\include\QtSvg" "-I$(QTDIR)\include\QtWidgets" "-I$(QTDIR)\include\QtXml"</Command>
    7. <AdditionalInputs Condition="'$(Configuration)|$(Platform)'=='Release|x64'">$(QTDIR)\bin\moc.exe;%(FullPath);$(QTDIR)\bin\moc.exe;%(FullPath)</AdditionalInputs>
    8. <Message Condition="'$(Configuration)|$(Platform)'=='Release|x64'">Moc%27ing MainWindow.h...</Message>
    9. <Outputs Condition="'$(Configuration)|$(Platform)'=='Release|x64'">.\GeneratedFiles\$(ConfigurationName)\moc_%(Filename).cpp</Outputs>
    10. <Command Condition="'$(Configuration)|$(Platform)'=='Release|x64'">"$(QTDIR)\bin\moc.exe" "%(FullPath)" -o ".\GeneratedFiles\$(ConfigurationName)\moc_%(Filename).cpp" -DUNICODE -DWIN32 -DWIN64 -DQT_DLL -DQT_NO_DEBUG -DNDEBUG -DQT_CORE_LIB -DQT_GUI_LIB -DQT_SQL_LIB -DQT_OPENGL_LIB -DQT_PRINTSUPPORT_LIB -DQT_SVG_LIB -DQT_WIDGETS_LIB -DQT_XML_LIB "-I.\GeneratedFiles" "-I." "-I$(QTDIR)\include" "-I.\GeneratedFiles\$(ConfigurationName)\." "-I$(QTDIR)\include\QtCore" "-I$(QTDIR)\include\QtGui" "-I$(QTDIR)\include\QtSql" "-I$(QTDIR)\include\QtOpenGL" "-I$(QTDIR)\include\QtPrintSupport" "-I$(QTDIR)\include\QtSvg" "-I$(QTDIR)\include\QtWidgets" "-I$(QTDIR)\include\QtXml"</Command>
    11. </CustomBuild>
    12. </ItemGroup>
    To copy to clipboard, switch view to plain text mode 

    If you understand how VS vcxproj files work, you can add these entries manually. There are more entries, too: to control compilation of the moc_*.cpp files, to compile the qrc file, and so on.

    A good way to see what is needed is to compare a plain MSVC C++ project file with an MSVC Qt C++ application project file. Just create a default project of each type and compare the vcxproj files in a text editor.

    I think you will find it is easiest to start over with a new Qt VS project and add your source files back into it.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  3. The following user says thank you to d_stranz for this useful post:

    enekow (17th November 2017)

Similar Threads

  1. Qt 5.9.2 WebEngine build fails with Visual Studio 2017
    By gvanvoor in forum Installation and Deployment
    Replies: 0
    Last Post: 9th November 2017, 13:12
  2. Replies: 1
    Last Post: 9th October 2017, 23:21
  3. What are the advantages of developing in Qt this 2017?
    By laurantx0ne in forum Qt Programming
    Replies: 3
    Last Post: 4th April 2017, 22:34

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.