diff options
author | Marc Mutz <marc.mutz@qt.io> | 2021-11-22 10:30:04 +0100 |
---|---|---|
committer | Marc Mutz <marc.mutz@qt.io> | 2021-11-27 01:25:10 +0100 |
commit | a0f9aef11bd3a692f9fa9c6069ca27200ef82893 (patch) | |
tree | 9b66bf55c56edabced026fe32f9a15ec1b361cb2 /src/corelib/kernel | |
parent | cf2336ae6e9b0dd0db9369d0a0bfaf9ecf393cf9 (diff) |
Long live Q_GADGET_EXPORT!
Like Q_NAMESPACE_EXPORT for Q_NAMESPACE, this variant of Q_GADGET
allows passing an export macro. This is useful to avoid exporting the
whole class just to get the staticMetaObject hidden therein exported.
Before anyone asks: No, we don't need Q_OBJECT_EXPORT, because QObject
subclasses, being polymorphic, always need to have a class-level
export macro (to export their vtable), but while that technique also
works for value classes (the Q_GADGET audience), it is not desirable
for them, because it makes inline functions exported in Windows debug
builds, which is not what we want, because it needlessly restricts
what you can to with the inline functions (e.g. remove).
[ChangeLog][QtCore] Added the Q_GADGET_EXPORT macro, which is like
Q_GADGET, but allows passing an export macro (like Q_NAMESPACE_EXPORT
for Q_NAMESPACE).
Fixes: QTBUG-55458
Change-Id: I546297de1e8aa45d83381991bcd3fbca61e1eef0
Reviewed-by: Edward Welbourne <edward.welbourne@qt.io>
Diffstat (limited to 'src/corelib/kernel')
-rw-r--r-- | src/corelib/kernel/qobject.cpp | 33 | ||||
-rw-r--r-- | src/corelib/kernel/qtmetamacros.h | 11 |
2 files changed, 37 insertions, 7 deletions
diff --git a/src/corelib/kernel/qobject.cpp b/src/corelib/kernel/qobject.cpp index d4b345c2d4..8d131cecc5 100644 --- a/src/corelib/kernel/qobject.cpp +++ b/src/corelib/kernel/qobject.cpp @@ -4400,8 +4400,8 @@ QDebug operator<<(QDebug dbg, const QObject *o) \since 5.5 This macro registers an enum type with the meta-object system. - It must be placed after the enum declaration in a class that has the Q_OBJECT or the - Q_GADGET macro. For namespaces use \l Q_ENUM_NS() instead. + It must be placed after the enum declaration in a class that has the Q_OBJECT, + Q_GADGET or Q_GADGET_EXPORT macro. For namespaces use \l Q_ENUM_NS() instead. For example: @@ -4519,7 +4519,7 @@ QDebug operator<<(QDebug dbg, const QObject *o) \snippet signalsandslots/signalsandslots.h 3 \note This macro requires the class to be a subclass of QObject. Use - Q_GADGET instead of Q_OBJECT to enable the meta object system's support + Q_GADGET or Q_GADGET_EXPORT instead of Q_OBJECT to enable the meta object system's support for enums in a class that is not a QObject subclass. \sa {Meta-Object System}, {Signals and Slots}, {Qt's Property System} @@ -4540,6 +4540,33 @@ QDebug operator<<(QDebug dbg, const QObject *o) Q_GADGET makes a class member, \c{staticMetaObject}, available. \c{staticMetaObject} is of type QMetaObject and provides access to the enums declared with Q_ENUMS. + + \sa Q_GADGET_EXPORT +*/ + +/*! + \macro Q_GADGET_EXPORT(EXPORT_MACRO) + \relates QObject + \since 6.3 + + The Q_GADGET_EXPORT macro works exactly like the Q_GADGET macro. + However, the \c{staticMetaObject} variable that is made available (see + Q_GADGET) is declared with the supplied \a EXPORT_MACRO qualifier. This is + useful if the object needs to be exported from a dynamic library, but the + enclosing class as a whole should not be (e.g. because it consists of mostly + inline functions). + + For example: + + \code + class Point { + Q_GADGET_EXPORT(EXPORT_MACRO) + Q_PROPERTY(int x MEMBER x) + Q_PROPERTY(int y MEMBER y) + ~~~ + \endcode + + \sa Q_GADGET, {Creating Shared Libraries} */ /*! diff --git a/src/corelib/kernel/qtmetamacros.h b/src/corelib/kernel/qtmetamacros.h index 96b1ada57e..fb4dfc5b0d 100644 --- a/src/corelib/kernel/qtmetamacros.h +++ b/src/corelib/kernel/qtmetamacros.h @@ -171,10 +171,10 @@ private: \ #define Q_OBJECT_FAKE Q_OBJECT QT_ANNOTATE_CLASS(qt_fake, "") #ifndef QT_NO_META_MACROS -/* qmake ignore Q_GADGET */ -#define Q_GADGET \ +/* qmake ignore Q_GADGET_EXPORT */ +#define Q_GADGET_EXPORT(...) \ public: \ - static const QMetaObject staticMetaObject; \ + static __VA_ARGS__ const QMetaObject staticMetaObject; \ void qt_check_for_QGADGET_macro(); \ typedef void QtGadgetHelper; \ private: \ @@ -185,7 +185,10 @@ private: \ QT_ANNOTATE_CLASS(qt_qgadget, "") \ /*end*/ -/* qmake ignore Q_NAMESPACE_EXPORT */ +/* qmake ignore Q_GADGET */ +#define Q_GADGET Q_GADGET_EXPORT() + + /* qmake ignore Q_NAMESPACE_EXPORT */ #define Q_NAMESPACE_EXPORT(...) \ extern __VA_ARGS__ const QMetaObject staticMetaObject; \ QT_ANNOTATE_CLASS(qt_qnamespace, "") \ |