Qt宏Q_OBJECT展开记录

时间:2024-06-08 19:03:02

c++中的宏

#define A 3.14 展开后把A替换为B

#define A(a) #a 展开后用a的字符串替换A(a)

#define A(a) a##B 展开后把a和B的字符串连接起来后替换A(a)

#include "common/common.h"
#define PI 3.14
#define PIPrivate 3.1415
#define foo(arg) #arg
#define bar(arg) arg##Private
int main(int argc, char *argv[]) { DApplication::loadDXcbPlugin();
DApplication app(argc, argv);
Dtk::Util::DLogManager::registerConsoleAppender();
Dtk::Util::DLogManager::registerFileAppender();
qDebug()<<PI<<foo(PI)<<bar(PI)<<foo(bar(PI));
return 0;
}

源码地址:GitHub

在Qt中,用了不少的宏。最常见的就是Q_OBJECT

Q_OBJECT展开的最终效果如下

public:
template<typename ThisObject>
inline void qt_check_for_QOBJECT_macro(const ThisObject &_q_argument) const {
int i = qYouForgotTheQ_OBJECT_Macro(this, &_q_argument);
i = i + 1;
}
_Pragma(GCC diagnostic push)
static const QMetaObject staticMetaObject;
virtual const QMetaObject *metaObject() const;
virtual void *qt_metacast(const char *);
virtual int qt_metacall(QMetaObject::Call, int, void **);
static inline QString tr(const char *s, const char *c = Q_NULLPTR, int n = -1) {
return staticMetaObject.tr(s, c, n);
} static inline QString trUtf8(const char *s, const char *c = Q_NULLPTR, int n = -1) {
return staticMetaObject.tr(s, c, n);
}
private:
__attribute__((visibility("hidden"))) static void qt_static_metacall(QObject *, QMetaObject::Call, int, void **);
_Pragma(GCC diagnostic pop)
struct QPrivateSignal {};

下面做一步步展开
Q_OBJECT展开后如下,又有不少的宏

public:
Q_OBJECT_CHECK
QT_WARNING_PUSH
Q_OBJECT_NO_OVERRIDE_WARNING
static const QMetaObject staticMetaObject;
virtual const QMetaObject *metaObject() const;
virtual void *qt_metacast(const char *);
virtual int qt_metacall(QMetaObject::Call, int, void **);
QT_TR_FUNCTIONS
private:
Q_OBJECT_NO_ATTRIBUTES_WARNING
Q_DECL_HIDDEN_STATIC_METACALL static void qt_static_metacall(QObject *, QMetaObject::Call, int, void **);
QT_WARNING_POP
struct QPrivateSignal {};
QT_ANNOTATE_CLASS(qt_qobject, "")

Q_OBJECT_CHECK展开如下,宏越来越多了。

template<typename ThisObject>
inline void qt_check_for_QOBJECT_macro(const ThisObject &_q_argument) const {
int i = qYouForgotTheQ_OBJECT_Macro(this, &_q_argument);
i = i + 1;
}

QT_WARNING_PUSHQT_WARNING_POP展开

#define QT_WARNING_PUSH QT_DO_PRAGMA(GCC diagnostic push)
#define QT_WARNING_POP QT_DO_PRAGMA(GCC diagnostic pop)

QT_DO_PRAGMA展开

#define QT_DO_PRAGMA(text)                      _Pragma(#text)

又来了新的宏_Pragma,参考2.1.3 _Pragma操作符,就是告诉编译器GCC diagnostic push

Q_OBJECT_NO_OVERRIDE_WARNING展开

#if defined(Q_CC_CLANG) && Q_CC_CLANG >= 306
# define Q_OBJECT_NO_OVERRIDE_WARNING QT_WARNING_DISABLE_CLANG("-Winconsistent-missing-override")
#else
# define Q_OBJECT_NO_OVERRIDE_WARNING
#endif

QT_TR_FUNCTIONS展开,QT_DEPRECATED展开为空

static inline QString tr(const char *s, const char *c = Q_NULLPTR, int n = -1) {
return staticMetaObject.tr(s, c, n);
} QT_DEPRECATED static inline QString
trUtf8(const char *s, const char *c = Q_NULLPTR, int n = -1) {
return staticMetaObject.tr(s, c, n);
}

Q_OBJECT_NO_ATTRIBUTES_WARNING展开,QT_WARNING_DISABLE_GCC展开为空

#define Q_OBJECT_NO_ATTRIBUTES_WARNING QT_WARNING_DISABLE_GCC("-Wattributes")

Q_DECL_HIDDEN_STATIC_METACALL展开,出现了__attribute__黑魔法,参考ATTRIBUTE 你知多少?

#define Q_DECL_HIDDEN_STATIC_METACALL Q_DECL_HIDDEN
#define Q_DECL_HIDDEN __attribute__((visibility("hidden")))

QT_ANNOTATE_CLASS展开为空

#define QT_ANNOTATE_CLASS(type, ...)

参考文章
2.1.3 _Pragma操作符
ATTRIBUTE 你知多少?

源码地址:GitHub

http://qtdream.com:1111/topic/988/qt%E5%AE%8Fq_object%E5%B1%95%E5%BC%80%E8%AE%B0%E5%BD%95/2