aboutsummaryrefslogtreecommitdiffstats
path: root/src/qml/qml/qqml.cpp
diff options
context:
space:
mode:
authorFabian Kosmale <fabian.kosmale@qt.io>2019-06-14 16:54:14 +0200
committerFabian Kosmale <fabian.kosmale@qt.io>2019-07-19 13:43:59 +0200
commitce545a5bf291eec7c623e6f691842eca18b09ddc (patch)
tree217bea662272f3295a9887d081acfac7adc04958 /src/qml/qml/qqml.cpp
parent4173415250b932184a9ab8f32fc55038e592e2c4 (diff)
Introduce qmlRegisterSingletonInstance
This method is intended as a replacement for common setContextProperty use cases, where the user is only using one single engine and has already created the object. Task-number: QTBUG-73064 Change-Id: Ib9ec023a0ad679aa22e90ebcb4a0c07622459c61 Reviewed-by: Simon Hausmann <simon.hausmann@qt.io>
Diffstat (limited to 'src/qml/qml/qqml.cpp')
-rw-r--r--src/qml/qml/qqml.cpp20
1 files changed, 20 insertions, 0 deletions
diff --git a/src/qml/qml/qqml.cpp b/src/qml/qml/qqml.cpp
index 613816e3f7..8a50b51b5d 100644
--- a/src/qml/qml/qqml.cpp
+++ b/src/qml/qml/qqml.cpp
@@ -76,6 +76,26 @@ int qmlTypeId(const char *uri, int versionMajor, int versionMinor, const char *q
return QQmlMetaType::typeId(uri, versionMajor, versionMinor, qmlName);
}
+// From qqmlprivate.h
+QObject *QQmlPrivate::RegisterSingletonFunctor::operator()(QQmlEngine *qeng, QJSEngine *)
+{
+ if (qeng->thread() != m_object->thread()) {
+ QQmlError error;
+ error.setDescription(QLatin1String("Registered object must live in the same thread as the engine it was registered with"));
+ QQmlEnginePrivate::get(qeng)->warning(qeng, error);
+ return nullptr;
+ }
+ if (alreadyCalled) {
+ QQmlError error;
+ error.setDescription(QLatin1String("Singleton registered by registerSingletonInstance must only be accessed from one engine"));
+ QQmlEnginePrivate::get(qeng)->warning(qeng, error);
+ return nullptr;
+ }
+ alreadyCalled = true;
+ qeng->setObjectOwnership(m_object, QQmlEngine::CppOwnership);
+ return m_object;
+};
+
/*
This method is "over generalized" to allow us to (potentially) register more types of things in
the future without adding exported symbols.