Home > Mobile >  Is Q_PROPERTY a function-like macro in C ?
Is Q_PROPERTY a function-like macro in C ?

Time:11-25

In my opinion,the using of a function-like macro in C is similar to the using of a common function. It seems to be like this:

macroFunctionName(arg1, arg2, arg3);

However, the using of Q_PROPERTY usually looks like this:

Q_PROPERTY(Qt::WindowModality windowModality READ windowModality WRITE setWindowModality)

As we can see, they are different.There is no comma in the using of Q_PROPERTY.I have never seen a function-like macro which was used like Q_PROPERTY.I am even not sure whether Q_PROPERTY is a function-like macro in C .So is it ill-formed in C ? Or it's just a special syntax for MOC in Qt?

I tried to find it in the C standard document but nothing about it was found.

CodePudding user response:

I looked in Qt's ./src/corelib/kernel/qobjectdefs.h file for the definition, and it looks like this:

#define Q_PROPERTY(...) QT_ANNOTATE_CLASS(qt_property, __VA_ARGS__)

... which would make Q_PROPERTY a variadic macro. Of course all it does is expand out to QT_ANNOTATE_CLASS, which is a different macro, one that Qt's moc utility presumably knows how to handle in a meaningful way when generating its moc_*.cpp files.

As for the use of spaces rather than commas; you're right, the preprocessor doesn't treat spaces as argument-separators. I suspect that the C preprocessor is simply passing the entire line (i.e. "Qt::WindowModality windowModality READ windowModality WRITE setWindowModality") into the QT_ANNOTATE_CLASS macro as a single argument, and that moc's QT_ANNOTATE_CLASS macro-definition is doing some stringification preprocessor tricks in order to parse it as a string-argument.

  • Related