diff --git a/src/controller.cpp b/src/controller.cpp index 629f84103..92defd35d 100644 --- a/src/controller.cpp +++ b/src/controller.cpp @@ -70,19 +70,21 @@ Controller::Controller(QObject *parent) Connection::setUserType(); #ifndef Q_OS_ANDROID - TrayIcon *trayIcon = new TrayIcon(this); if (NeoChatConfig::self()->systemTray()) { - trayIcon->show(); - connect(trayIcon, &TrayIcon::showWindow, this, &Controller::showWindow); + m_trayIcon = new TrayIcon(this); + m_trayIcon->show(); + connect(m_trayIcon, &TrayIcon::showWindow, this, &Controller::showWindow); QGuiApplication::setQuitOnLastWindowClosed(false); } - connect(NeoChatConfig::self(), &NeoChatConfig::SystemTrayChanged, this, [this, trayIcon]() { + connect(NeoChatConfig::self(), &NeoChatConfig::SystemTrayChanged, this, [this]() { if (NeoChatConfig::self()->systemTray()) { - trayIcon->show(); - connect(trayIcon, &TrayIcon::showWindow, this, &Controller::showWindow); + m_trayIcon = new TrayIcon(this); + m_trayIcon->show(); + connect(m_trayIcon, &TrayIcon::showWindow, this, &Controller::showWindow); } else { - trayIcon->hide(); - disconnect(trayIcon, &TrayIcon::showWindow, this, &Controller::showWindow); + disconnect(m_trayIcon, &TrayIcon::showWindow, this, &Controller::showWindow); + delete m_trayIcon; + m_trayIcon = nullptr; } QGuiApplication::setQuitOnLastWindowClosed(!NeoChatConfig::self()->systemTray()); }); diff --git a/src/controller.h b/src/controller.h index 889f38ac4..eef1342fd 100644 --- a/src/controller.h +++ b/src/controller.h @@ -19,6 +19,7 @@ class QKeySequences; class NeoChatRoom; class NeoChatUser; +class TrayIcon; class QQuickWindow; namespace QKeychain @@ -100,6 +101,7 @@ private: QPointer m_connection; bool m_busy = false; + TrayIcon *m_trayIcon = nullptr; static QByteArray loadAccessTokenFromFile(const AccountSettings &account); QKeychain::ReadPasswordJob *loadAccessTokenFromKeyChain(const AccountSettings &account);