summaryrefslogtreecommitdiff
path: root/src/manager-lib/qml-utilities.cpp
diff options
context:
space:
mode:
authorRobert Griebl <robert.griebl@pelagicore.com>2015-10-29 01:12:27 +0100
committerRobert Griebl <robert.griebl@pelagicore.com>2015-11-02 10:14:00 +0000
commitc606a37aa4c8968a92d0fcaf27cedbf311397754 (patch)
tree467134131b21d85de38c6c0ffa2de593d560a96a /src/manager-lib/qml-utilities.cpp
parent5a4d9e558c02a32114e98dcae2307eef688a7eb4 (diff)
downloadqtapplicationmanager-c606a37aa4c8968a92d0fcaf27cedbf311397754.tar.gz
Finally applied Dominik's QML singleton ownership workaround.
QML expects C++ singletons to be owned by the QML engine, but this is not always desireable. Simon has already acknowledged that we should add an public API in Qt for that, but in the meantime this is the only way to do it. Change-Id: I734cd77214c3b91e701dae62bc9712c0b85de718 Reviewed-by: Dominik Holland <dominik.holland@pelagicore.com> Reviewed-by: Laszlo Agocs <laszlo.agocs@theqtcompany.com>
Diffstat (limited to 'src/manager-lib/qml-utilities.cpp')
-rw-r--r--src/manager-lib/qml-utilities.cpp48
1 files changed, 48 insertions, 0 deletions
diff --git a/src/manager-lib/qml-utilities.cpp b/src/manager-lib/qml-utilities.cpp
new file mode 100644
index 00000000..c3e692db
--- /dev/null
+++ b/src/manager-lib/qml-utilities.cpp
@@ -0,0 +1,48 @@
+/****************************************************************************
+**
+** Copyright (C) 2015 Pelagicore AG
+** Contact: http://www.pelagicore.com/
+**
+** This file is part of the Pelagicore Application Manager.
+**
+** $QT_BEGIN_LICENSE:LGPL3$
+** Commercial License Usage
+** Licensees holding valid commercial Pelagicore Application Manager
+** licenses may use this file in accordance with the commercial license
+** agreement provided with the Software or, alternatively, in accordance
+** with the terms contained in a written agreement between you and
+** Pelagicore. For licensing terms and conditions, contact us at:
+** http://www.pelagicore.com.
+**
+** GNU General Public License Usage
+** Alternatively, this file may be used under the terms of the GNU
+** General Public License version 3 as published by the Free Software
+** Foundation and appearing in the file LICENSE.GPLv3 included in the
+** packaging of this file. Please review the following information to
+** ensure the GNU General Public License version 3 requirements will be
+** met: http://www.gnu.org/licenses/gpl-3.0.html.
+**
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+
+#include <QTimer>
+#include <private/qqmlmetatype_p.h>
+
+#include "qml-utilities.h"
+
+void retakeSingletonOwnershipFromQmlEngine(QQmlEngine *qmlEngine, QObject *singleton)
+{
+ // QQmlEngine is taking ownership of singletons after the first call to instanceForQml() and
+ // there is nothing to prevent this. This means that the singleton will be destroyed once the
+ // QQmlEngine is destroyed, which is not what we want.
+ // The workaround (until Qt has an API for that) is to remove the singleton QObject from QML's
+ // internal singleton registry *after* the instanceForQml() function has finished.
+
+ QTimer::singleShot(0, qmlEngine, [qmlEngine, singleton]() {
+ foreach (const QQmlType *singletonType, QQmlMetaType::qmlSingletonTypes()) {
+ if (singletonType->singletonInstanceInfo()->qobjectApi(qmlEngine) == singleton)
+ singletonType->singletonInstanceInfo()->qobjectApis.remove(qmlEngine);
+ }
+ });
+}