Search code examples
qtqt4preprocessormoc

Macro expansion in moc


I'd like to store some class info using Q_CLASSINFO macro. However I would like to wrap it in my own macro, for example:

#define DB_TABLE( TABLE ) \
    Q_CLASSINFO( "db_table", #TABLE )

#define DB_FIELD( PROPERTY, COLUMN ) \
    Q_CLASSINFO( "dbcol_" #PROPERTY, #COLUMN )

class Foo : public QObject
{
    Q_OBJECT
    DB_TABLE( some_table )
    DB_FIELD( clientName, client_name )
}

Unfortunately, moc doesn't expand macros so the Q_CLASSINFO is not added.

I've tried to feed moc with already preprocessed source, but it failes on some included Qt classes.

Do you know any workaround for this?


Solution

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