summaryrefslogtreecommitdiffstats
path: root/src/corelib/kernel
diff options
context:
space:
mode:
authorGiuseppe D'Angelo <giuseppe.dangelo@kdab.com>2019-04-30 09:39:21 +0200
committerGiuseppe D'Angelo <giuseppe.dangelo@kdab.com>2019-06-28 06:47:29 +0200
commitf66c1db16c050c9d685a44a38ad7c5cf9f6fcc96 (patch)
tree53e38e8714d493e321e0437f4d4f59bd6f4aff10 /src/corelib/kernel
parent8f0e3ad5188d17c88f624eeea9a67ab17e55cc6f (diff)
Introduce Q_NAMESPACE_EXPORT
A recurring problem with the Q_NAMESPACE macro is that it declares an object (staticMetaObject) in the surrounding namespace. That object lacks any export/import qualification to make it usable with shared libraries. Introduce therefore another macro to work around this issue, allowing the user to prefix the object with an exporting macro, f.i. like this: Q_NAMESPACE_EXPORT(Q_CORE_EXPORT) The old macro can simply then be rewritten in terms of this new one, supplying an empty export macro. Note that NOT passing an argument to a macro expecting one is well defined behavior in C99 -- the macro will expand an empty token. Of course, MSVC doesn't like this and emits warnings. As a workaround, use a variadic macro. [ChangeLog][QtCore] Added the new Q_NAMESPACE_EXPORT macro. It can be used just like Q_NAMESPACE to add meta-object information to a namespace; however it also supports exporting of such information from shared libraries. [ChangeLog][Potentially Source-Incompatible Changes] Prefixing Q_NAMESPACE with an export macro may no longer work. Use the new Q_NAMESPACE_EXPORT macro for that use case. Fixes: QTBUG-68014 Change-Id: Ib044a555ace1f77ae8e0244d824ec473550f3d8e Reviewed-by: Edward Welbourne <edward.welbourne@qt.io> Reviewed-by: Qt CI Bot <qt_ci_bot@qt-project.org> Reviewed-by: BogDan Vatra <bogdan@kdab.com>
Diffstat (limited to 'src/corelib/kernel')
-rw-r--r--src/corelib/kernel/qobject.cpp18
-rw-r--r--src/corelib/kernel/qobjectdefs.h10
2 files changed, 25 insertions, 3 deletions
diff --git a/src/corelib/kernel/qobject.cpp b/src/corelib/kernel/qobject.cpp
index e3b25f8bf7..1540ebfe12 100644
--- a/src/corelib/kernel/qobject.cpp
+++ b/src/corelib/kernel/qobject.cpp
@@ -4526,6 +4526,24 @@ QDebug operator<<(QDebug dbg, const QObject *o)
Q_NAMESPACE makes an external variable, \c{staticMetaObject}, available.
\c{staticMetaObject} is of type QMetaObject and provides access to the
enums declared with Q_ENUM_NS/Q_FLAG_NS.
+
+ \sa Q_NAMESPACE_EXPORT
+*/
+
+/*!
+ \macro Q_NAMESPACE_EXPORT(EXPORT_MACRO)
+ \relates QObject
+ \since 5.14
+
+ The Q_NAMESPACE_EXPORT macro can be used to add QMetaObject capabilities
+ to a namespace.
+
+ It works exactly like the Q_NAMESPACE macro. However, the external
+ \c{staticMetaObject} variable that gets defined in the namespace
+ is declared with the supplied \c{EXPORT_MACRO} qualifier. This is
+ useful f.i. if the object needs to be exported from a dynamic library.
+
+ \sa Q_NAMESPACE, {Creating Shared Libraries}
*/
/*!
diff --git a/src/corelib/kernel/qobjectdefs.h b/src/corelib/kernel/qobjectdefs.h
index 4d5ac4dcb2..ef22b6e67f 100644
--- a/src/corelib/kernel/qobjectdefs.h
+++ b/src/corelib/kernel/qobjectdefs.h
@@ -201,12 +201,16 @@ private: \
QT_ANNOTATE_CLASS(qt_qgadget, "") \
/*end*/
-/* qmake ignore Q_NAMESPACE */
-#define Q_NAMESPACE \
- extern const QMetaObject staticMetaObject; \
+/* qmake ignore Q_NAMESPACE_EXPORT */
+#define Q_NAMESPACE_EXPORT(...) \
+ extern __VA_ARGS__ const QMetaObject staticMetaObject; \
QT_ANNOTATE_CLASS(qt_qnamespace, "") \
/*end*/
+/* qmake ignore Q_NAMESPACE */
+#define Q_NAMESPACE Q_NAMESPACE_EXPORT() \
+ /*end*/
+
#endif // QT_NO_META_MACROS
#else // Q_MOC_RUN