From 8c0fbf2125ef368835d491cded83821b861c8f5f Mon Sep 17 00:00:00 2001 From: Odizinne Date: Sun, 15 Sep 2024 17:12:09 +0200 Subject: [PATCH] fixed crash with tray menu --- src/HeadsetControlQt/headsetcontrolqt.cpp | 31 ++++++++++++++--------- src/HeadsetControlQt/headsetcontrolqt.h | 4 +++ 2 files changed, 23 insertions(+), 12 deletions(-) diff --git a/src/HeadsetControlQt/headsetcontrolqt.cpp b/src/HeadsetControlQt/headsetcontrolqt.cpp index e2c5b3b..7a5311f 100644 --- a/src/HeadsetControlQt/headsetcontrolqt.cpp +++ b/src/HeadsetControlQt/headsetcontrolqt.cpp @@ -43,7 +43,6 @@ HeadsetControlQt::HeadsetControlQt(QWidget *parent) setWindowIcon(QIcon(":/icons/icon.png")); initUI(); createTrayIcon(); - createTrayMenu(); loadSettings(); setupUIConnections(); updateHeadsetInfo(); @@ -142,20 +141,19 @@ void HeadsetControlQt::checkStartupCheckbox() void HeadsetControlQt::createTrayIcon() { trayIcon->setIcon(QIcon(":/icons/icon.png")); - trayIcon->show(); - connect(trayIcon, &QSystemTrayIcon::activated, this, &HeadsetControlQt::trayIconActivated); -} + trayMenu = new QMenu(this); + showAction = new QAction(tr("Show"), this); + exitAction = new QAction(tr("Exit"), this); -void HeadsetControlQt::createTrayMenu() -{ - QMenu *trayMenu = new QMenu(this); - QAction *showAction = new QAction(tr("Show"), this); connect(showAction, &QAction::triggered, this, &HeadsetControlQt::toggleWindow); - trayMenu->addAction(showAction); - QAction *exitAction = new QAction(tr("Exit"), this); connect(exitAction, &QAction::triggered, this, &QApplication::quit); + + trayMenu->addAction(showAction); trayMenu->addAction(exitAction); trayIcon->setContextMenu(trayMenu); + trayIcon->show(); + + connect(trayIcon, &QSystemTrayIcon::activated, this, &HeadsetControlQt::trayIconActivated); } void HeadsetControlQt::createDefaultSettings() @@ -580,7 +578,16 @@ void HeadsetControlQt::changeApplicationLanguage() } ui->retranslateUi(this); - delete trayIcon->contextMenu(); - createTrayMenu(); + updateTrayMenu(); saveSettings(); } + +void HeadsetControlQt::updateTrayMenu() +{ + if (this->isVisible()) { + showAction->setText(tr("Hide")); + } else { + showAction->setText(tr("Show")); + } + exitAction->setText(tr("Exit")); +} diff --git a/src/HeadsetControlQt/headsetcontrolqt.h b/src/HeadsetControlQt/headsetcontrolqt.h index 1692bca..9e8a027 100644 --- a/src/HeadsetControlQt/headsetcontrolqt.h +++ b/src/HeadsetControlQt/headsetcontrolqt.h @@ -60,6 +60,7 @@ private slots: void noDeviceFound(); void toggleUIElements(bool show); void sendFirstMinimizeNotification(); + void updateTrayMenu(); static const QString settingsFile; static const QString headsetcontrolExecutable; static const QString desktopFile; @@ -73,6 +74,9 @@ private slots: bool soundNotificationSent; bool firstRun; bool closing; + QMenu *trayMenu; + QAction *exitAction; + QAction *showAction; QThread workerThread; Worker *worker;