Search code examples
qtc-preprocessormoc

C++ Preprocessor and QT MOC


I am attempting something (perhaps stupid).

Have used some macroes to create "amounts" of functions in C++ domain;

#define THR_CONFIG_VALUE(path, value, type, name, defaultvalue) \
    type name() { return m_##name; } \
    void set##name(type data) { m_##name = data; }
#include <backend/config.i>
#undef THR_CONFIG_VALUE

Based upon following config.i file:

THR_CONFIG_VALUE("", "type", int, ThisType, 0)
THR_CONFIG_VALUE("", "auto", bool, AutoVar1, false)
THR_CONFIG_VALUE("", "auto", bool, AutoVar2, false)

That works fine, i get a number of getter setter functions, also generate member variables the same way (for the record).

Now I start mixing up with QT stuff, trying to use the MOC to generate the Q_PROPERTIES:

#define THR_CONFIG_VALUE(path, value, type, name, defaultvalue) \
    Q_PROPERTY(type name READ name WRITE set##name NOTIFY indexChanged)
#include <backend/vessel/thruster/thruster_config.i>
#undef THR_CONFIG_VALUE

The MOC do not care for such attempts. This would have saved me typing 170 Q_PROPERTY lines and in the future several 100's more.

Question 1: Why, preprocessor and MOC sequence? Question 2: Is there a "QT way" ?

Thanks,


Solution

  • Now i Have an implementation that works. Used Verdigris's contribution.

    A little bit tricky, had to enable c++14 and also add this define to the PRO file:

    DEFINES += __cpp_constexpr=201304 __cpp_variable_templates=201304
    

    Also as i used namespaces; had to do the includes inside the namespace:

    FRONTEND_BEGIN_NAMESPACE
    
    #include <frontend/gui/helper/wobjectdefs.h>
    

    Then it works:

    #define THR_CONFIG_VALUE(path, value, type, name, defaultvalue) \
        W_PROPERTY(type, name MEMBER m_##name) 
    #include <backend/config.i>
    #undef THR_CONFIG_VALUE