Problems with QMenu and QSystemTrayIcon on macOS
-
I have small system tray application like this:
TrayApp::TrayApp(QObject *parent) : QObject(parent) { m_trayIcon = new QSystemTrayIcon(this); m_trayMenu = new QMenu(); m_subMenu = new QMenu("Submenu", m_trayMenu); connect(m_trayMenu, &QMenu::aboutToShow, this, &TrayApp::rebuildTrayMenu); m_trayIcon->setContextMenu(m_trayMenu); m_trayIcon->show(); } void TrayApp::rebuildTrayMenu() { m_trayMenu->clear(); m_subMenu->addAction("Action_1"); m_subMenu->addAction("Action_2"); m_trayMenu->addMenu(m_subMenu); m_trayMenu->addSeparator(); m_trayMenu->addAction(tr("Exit"), this, &TrayApp::onExitTriggered); }When I open subMenu for the first time (from the moment of launch until opening subMenu, no menu elements were activated) the entire menu disappears. I tried Python - same result. Qt 6.10.2, macOS 26.3
-
Hi and welcome to devnet,
Can you provide a complete minimal compilable reproducer please ?
This will allow people to test in the same conditions as you. -
TrayApp.hpp
#ifndef __TRAY_APP_HPP__ #define __TRAY_APP_HPP__ //need include QMessageBox //need include QSystemTrayIcon //need include QObject class TrayApp : public QObject { Q_OBJECT public: explicit TrayApp(QObject *parent = nullptr); ~TrayApp(); private slots: void rebuildTrayMenu(); void onExitTriggered(); private: QSystemTrayIcon *m_trayIcon = nullptr; QMenu *m_trayMenu = nullptr; QMenu *m_subMenu = nullptr; }; #endif // __TRAY_APP_HPP__TrayApp.cpp
#include "TrayApp.hpp" #include <QApplication> TrayApp::TrayApp(QObject *parent) : QObject(parent) { m_trayIcon = new QSystemTrayIcon(this); m_trayIcon->setToolTip(QStringLiteral("Tray App")); m_trayIcon->setIcon(QIcon::fromTheme("system-software-update")); m_trayMenu = new QMenu(); m_subMenu = new QMenu("Sub Menu", m_trayMenu); connect(m_trayMenu, &QMenu::aboutToShow, this, &TrayApp::rebuildTrayMenu); m_trayIcon->setContextMenu(m_trayMenu); m_trayIcon->show(); } TrayApp::~TrayApp() { delete m_subMenu; delete m_trayMenu; delete m_trayIcon; } void TrayApp::rebuildTrayMenu() { m_trayMenu->clear(); m_subMenu->clear(); m_subMenu->addAction(tr("Item 1")); m_subMenu->addAction(tr("Item 2")); m_trayMenu->addMenu(m_subMenu); m_trayMenu->addSeparator(); m_trayMenu->addAction(tr("Exit"), this, &TrayApp::onExitTriggered); } void TrayApp::onExitTriggered() { QApplication::quit(); }main.cpp
#include "TrayApp.hpp" #include <QApplication> int main(int argc, char *argv[]) { QApplication app(argc, argv); app.setQuitOnLastWindowClosed(false); app.setOrganizationName(QStringLiteral("Tray App")); app.setApplicationName(QStringLiteral("Sloth")); if (!QSystemTrayIcon::isSystemTrayAvailable()) { QMessageBox::critical(nullptr, QStringLiteral("Systray Error"), QStringLiteral("System tray is not available on this computer.")); return 1; } TrayApp trayApp; return app.exec(); }CMakeLists.txt
cmake_minimum_required(VERSION 3.16) project(TrayApp LANGUAGES CXX) set(CMAKE_CXX_STANDARD 17) find_package(Qt6 REQUIRED COMPONENTS Core Gui Widgets Quick Qml) set(CMAKE_AUTOMOC ON) set(CMAKE_AUTORCC ON) set(HEADERS TrayApp.hpp ) set(SOURCES main.cpp TrayApp.cpp ) add_executable(${PROJECT_NAME} ${HEADERS} ${SOURCES}) target_link_libraries(${PROJECT_NAME} PRIVATE Qt6::Core Qt6::Gui Qt6::Widgets Qt6::Quick Qt6::Qml ) target_include_directories(${PROJECT_NAME} PRIVATE ${CMAKE_SOURCE_DIR}) -
@SGaist
It looks like the issue is macOS-specific. Everything works fine on Sequoia 15.7.3. The issue is with Tahoe 26.3. -
26.3 might have broken something... Did you already check the bug report system ?