Qt Moc with Implementations Inside of Header Files

Generate moc file for header only class outside the project tree

I find the solution finally.

I simply added the HEADER command at the end of the .pri file.
Now the compiler scan the headers and create the moc file. ``

My update .pri file is:

INCLUDEPATH += $$PWD
DEPENDPATH += $$PWD

HEADERS += \
$$PWD/MyClass1.h \
$$PWD/MyClass2.h

Passing arguments from qmake to moc per header

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
...

What's the difference between CLASS.moc and moc_CLASS.cpp

<CLASS>.moc is generated wherever there is a need to MOC the source file as well as/instead of the header (i.e. if the Q_OBJECT macro is used in the source).

See this answer for how this can be achieved.

Macro expansion in moc

Other than rolling your own pre-moc preprocessor, no. That is what MeeGo Touch does, for example. Since Nokia themselves are doing it, I believe there is no other way.

In your case, it would only involve translating your own declarations into Q_CLASSINFO, so it shouldn't be too hard. If you use qmake, it can be added to the build sequence, too.



Related Topics



Leave a reply



Submit