Search code examples
visual-studio-2015qt5moc

Qt5 moc not generating file


I'm making a c++ plugin for maya as a dynamic library using VS2015, until now, I had no usage of Qt5, but I now need the Q_Gadget and Q_Enum functionality from Qt5.6.1 so I started to setup my vs to include qt, everything is compiling and I have no problem to use any of Qt class in my library, but when I try to use any of the qt's property system (Q_GADGET, Q_ENUM, etc), the moc don't seem to generate any file, I'm using the Qt VS tools 2.

I've read somewhere that I should convert my project to a Qt project, but the explanation on how to do it are very unclear, and i'm not sure if I can actually do it for my current project who have a huge amount of file.

is there a way to setup the moc to run over all my .h and to include the generated file automatically ?

Thanks in advance.


Solution

  • Could fix my issues using help provided on this page :

    automatic mocing in visual studio

    We create a VS property file (.props) inside the project with custom target before ClCompile that will load all .hpp file and execute moc.exe on them

    the .props modified for my 5.6.1 project :

    <?xml version="1.0" encoding="utf-8"?> 
    <Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
      <PropertyGroup>
        <SourceDir>$(ProjectDir)</SourceDir>
        <CppDir>$(SourceDir)/</CppDir>
        <GenDir>$(SourceDir)/Generated</GenDir>
      </PropertyGroup>
    
      <PropertyGroup>
        <QtDir>D:\Qt\Qt5.6.1\5.6\msvc2015</QtDir>
        <MocExt>hpp</MocExt>
        <Moc>$(QtDir)/bin/moc.exe</Moc>
      </PropertyGroup>
    
      <Target Inputs="@(QtIncludes)" 
              Name="Moc" 
              BeforeTargets="ClCompile" 
              Outputs="@(QtIncludes->'$(GenDir)/moc_%(Filename).cpp')">
        <Exec Command = "$(Moc) &quot;%(QtIncludes.identity)&quot; -nw -o $(GenDir)/moc_%(Filename).cpp $(MocFlags)"/>
      </Target>
      <Target Name="CreateDirectories" BeforeTargets="Moc">
        <MakeDir
            Directories="$(GenDir)"/>
      </Target>
    
      <Target Inputs="@(CopyToOutput)"
              Name="CopytoOut"
              Outputs="@(CopyToOutput->'%(DestinationFolder)/%(RecursiveDir)%(Filename)%(Extension)')"
              AfterTargets="Link">
      </Target>
      <ItemDefinitionGroup />
      <ItemGroup>
        <QtIncludes Include="$(ProjectDir)/**/*.$(MocExt)"/>
      </ItemGroup>
    </Project>