Fix crash caused by setting Tray window screen pointer on the QML side
authorMichael Schuster <michael@schuster.ms>
Thu, 4 Jun 2020 18:26:39 +0000 (20:26 +0200)
committerKevin Ottens (Rebase PR Action) <er-vin@users.noreply.github.com>
Mon, 15 Jun 2020 12:32:25 +0000 (12:32 +0000)
Seems like Qt sometimes doesn't like the QML window's screen property to be set
to a C++ (QVariant) pointer value, so we use the index: Qt.application.screens[]

See Qt docs: https://doc.qt.io/qt-5/qml-qtquick-window-window.html#screen-prop

This fix returns the matching window's index from the QGuiApplication::screens()
list to the QML side, instead of the window pointer.

Steps to reproduce the crash with the previous code:
- Open the Tray menu and close it a few times,
  or scroll randomly up and down in its activity list.

Tested with Qt 5.12.5

Signed-off-by: Michael Schuster <michael@schuster.ms>
src/gui/systray.cpp
src/gui/systray.h
src/gui/tray/Window.qml

index 97f19765c31abe9e0f2117d6743826eb9c374d31..ebcefc4df302f4c1066b53412c5df589f24232e5 100644 (file)
@@ -158,9 +158,11 @@ QScreen *Systray::currentScreen() const
     return nullptr;
 }
 
-QVariant Systray::currentScreenVar() const
+int Systray::currentScreenIndex() const
 {
-    return QVariant::fromValue(currentScreen());
+    const auto screens = QGuiApplication::screens();
+    const auto screenIndex = screens.indexOf(currentScreen());
+    return screenIndex > 0 ? screenIndex : 0;
 }
 
 Systray::TaskBarPosition Systray::taskbarOrientation() const
index feb0771aafa2dd9ddba0bd7bfcd589795ad72a15..0cb9249f6c3f926bd505c1a2a2666cd817f3d407 100644 (file)
@@ -58,7 +58,7 @@ public:
     Q_INVOKABLE bool syncIsPaused();
     Q_INVOKABLE void setOpened();
     Q_INVOKABLE void setClosed();
-    Q_INVOKABLE QVariant currentScreenVar() const;
+    Q_INVOKABLE int currentScreenIndex() const;
     Q_INVOKABLE QPoint calcTrayIconCenter() const;
     Q_INVOKABLE TaskBarPosition taskbarOrientation() const;
     Q_INVOKABLE QRect taskbarGeometry() const;
index 9e11828b8a042082c8a58d970f887d7d4541303a..2ec850d4afb539024e2f0f224d9f5b097cd9cc82 100644 (file)
@@ -63,7 +63,7 @@ Window {
         onShowWindow: {\r
             accountMenu.close();\r
 \r
-            trayWindow.screen = systrayBackend.currentScreenVar();\r
+            trayWindow.screen = Qt.application.screens[systrayBackend.currentScreenIndex()];\r
 \r
             var position = systrayBackend.computeWindowPosition(trayWindow.width, trayWindow.height)\r
             trayWindow.x = position.x\r