Use QQmlApplicationEngine
authorNicolas Fella <nicolas.fella@gmx.de>
Sun, 22 Mar 2020 15:14:26 +0000 (16:14 +0100)
committerNicolas Fella <nicolas.fella@gmx.de>
Sun, 22 Mar 2020 15:14:56 +0000 (16:14 +0100)
This simplifies the code a bit.

Signed-off-by: Nicolas Fella <nicolas.fella@gmx.de>
src/gui/systray.cpp
src/gui/systray.h

index 081a90b0e17d00763450724e32f80c2110b8c2e5..dc9ca6bed5416db5ea318567c35ee5e19b387703 100644 (file)
@@ -22,7 +22,7 @@
 #include <QDesktopServices>
 #include <QGuiApplication>
 #include <QQmlComponent>
-#include <QQmlEngine>
+#include <QQmlApplicationEngine>
 #include <QScreen>
 
 #ifdef USE_FDO_NOTIFICATIONS
@@ -50,20 +50,14 @@ Systray *Systray::instance()
 Systray::Systray()
     : _isOpen(false)
     , _syncIsPaused(false)
-    , _trayComponent(nullptr)
-    , _trayContext(nullptr)
 {
-    // Create QML tray engine, build component, set C++ backend context used in window.qml
-    // Use pointer instead of engine() helper function until Qt 5.12 is minimum standard
-    _trayEngine = new QQmlEngine;
+    _trayEngine = new QQmlApplicationEngine;
     _trayEngine->addImportPath("qrc:/qml/theme");
     _trayEngine->addImageProvider("avatars", new ImageProvider);
     _trayEngine->rootContext()->setContextProperty("userModelBackend", UserModel::instance());
     _trayEngine->rootContext()->setContextProperty("appsMenuModelBackend", UserAppsModel::instance());
     _trayEngine->rootContext()->setContextProperty("systrayBackend", this);
 
-    _trayComponent = new QQmlComponent(_trayEngine, QUrl(QStringLiteral("qrc:/qml/src/gui/tray/Window.qml")));
-
     connect(UserModel::instance(), &UserModel::newUserSelected,
         this, &Systray::slotNewUserSelected);
 
@@ -73,13 +67,11 @@ Systray::Systray()
 
 void Systray::create()
 {
-    if (_trayContext == nullptr) {
-        if (!AccountManager::instance()->accounts().isEmpty()) {
-            _trayEngine->rootContext()->setContextProperty("activityModel", UserModel::instance()->currentActivityModel());
-        }
-        _trayContext = _trayEngine->contextForObject(_trayComponent->create());
-        hideWindow();
+    if (!AccountManager::instance()->accounts().isEmpty()) {
+        _trayEngine->rootContext()->setContextProperty("activityModel", UserModel::instance()->currentActivityModel());
     }
+    _trayEngine->load(QStringLiteral("qrc:/qml/src/gui/tray/Window.qml"));
+    hideWindow();
 }
 
 void Systray::slotNewUserSelected()
index d562dcf426aade93ceeb10a740d024265c913808..9900c2d2ef87a70354cacb9a45b2907e2932d60a 100644 (file)
@@ -22,6 +22,7 @@
 #include "tray/UserModel.h"
 
 class QIcon;
+class QQmlApplicationEngine;
 
 namespace OCC {
 
@@ -78,9 +79,7 @@ private:
     Systray();
     bool _isOpen;
     bool _syncIsPaused;
-    QQmlEngine *_trayEngine;
-    QQmlComponent *_trayComponent;
-    QQmlContext *_trayContext;
+    QQmlApplicationEngine *_trayEngine;
 };
 
 } // namespace OCC