Search code examples
qtqt-creatorqmakemoc

Passing arguments from qmake to moc per header


I'd like to be able to pass arguments to the moc compiler from a qmake project, but only for specific files.

Example, in some .pro file:

HEADER += foo.h \
          bar.h \
          baz.h

I'd like to pass -ffoo_extra.h to foo.h, -fbar_extra.h to bar.h, and nothing to baz.h

This answer suggests using the QMAKE_MOC variable, but that affects all headers together. Something like that but targeted to individual header files would be ideal.


Solution

  • The -f argument adds an include to the generated output. You can achieve the same by leveraging the Q_MOC_RUN macro. It's even documented - together with command line options, no less :)

    // foo.h
    #ifdef Q_MOC_RUN
    #include "foo_extra.h"
    #endif
    ...
    
    // bar.h
    #ifdef Q_MOC_RUN
    #include "bar_extra.h"
    #endif
    ...