From 7ea4c59f46bf3bb2c126ab759dc00ae1bd73e726 Mon Sep 17 00:00:00 2001 From: ReenigneArcher <42013603+ReenigneArcher@users.noreply.github.com> Date: Mon, 11 May 2026 21:59:02 -0400 Subject: [PATCH] Provide default argv, hide menu, use popup Add a persistent default argv (anonymous namespace) and include QCursor, then construct QApplication with the default argv when original argc/argv are missing to avoid using a short-lived stack array. Hide trayTopMenu before clearing tray references to prevent lingering UI. Replace menu->show() with menu->popup(QCursor::pos()) to work around QTBUG-139921 on Linux/Wayland. NOSONAR comments for QApplication allocation are preserved. --- src/QtTrayMenu.cpp | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/src/QtTrayMenu.cpp b/src/QtTrayMenu.cpp index 2401e84..023b375 100644 --- a/src/QtTrayMenu.cpp +++ b/src/QtTrayMenu.cpp @@ -1,9 +1,16 @@ #include "QtTrayMenu.h" #include +#include #include #include +namespace { + int defaultArgc = 1; + char defaultArgv0[] = "TrayMenuApp"; + char *defaultArgv[] = {defaultArgv0, nullptr}; +} // namespace + QtTrayMenu::QtTrayMenu(QObject *parent): QtTrayMenu(-1, nullptr, false, parent) { }; @@ -19,11 +26,10 @@ QtTrayMenu::QtTrayMenu(int argc, char **argv, const bool debug, QObject *parent) // Note: The following is ugly but QApplication requires an argv containing the application name. // We might not have access to the real argc/argv here due to being called/pulled as a dependency. if (argc < 0 && argv == nullptr) { - argc = 1; - char *argvArray[] = {(char *) "TrayMenuApp", nullptr}; - argv = &argvArray[0]; + app = new QApplication(defaultArgc, defaultArgv); // NOSONAR(cpp:S5025) - Qt has its own integrated memory management + } else { + app = new QApplication(argc, argv); // NOSONAR(cpp:S5025) - Qt has its own integrated memory management } - app = new QApplication(argc, argv); // NOSONAR(cpp:S5025) - Qt has its own integrated memory management } if (debug) { app->installEventFilter(this); @@ -109,6 +115,7 @@ void QtTrayMenu::exit() { running = false; // Remove tray menu references if (trayTopMenu) { + trayTopMenu->hide(); if (trayIcon) { trayIcon->setContextMenu(nullptr); } @@ -234,7 +241,7 @@ void QtTrayMenu::showMenu() const { if (QMenu *menu = trayIcon->contextMenu(); menu != nullptr) { // Due to QTBUG-139921 this is currently not working on Linux/Wayland // with Qt-6.9+ unless menu has a transient parent (which we do not have here). - menu->show(); + menu->popup(QCursor::pos()); } }