Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Problems with QMenu and QSystemTrayIcon on macOS

Problems with QMenu and QSystemTrayIcon on macOS

Scheduled Pinned Locked Moved Unsolved General and Desktop
5 Posts 2 Posters 119 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • O Offline
    O Offline
    Oleh Herashchenko
    wrote last edited by
    #1

    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

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote last edited by
      #2

      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.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • O Offline
        O Offline
        Oleh Herashchenko
        wrote last edited by Oleh Herashchenko
        #3

        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})
        
        
        1 Reply Last reply
        0
        • O Offline
          O Offline
          Oleh Herashchenko
          wrote last edited by
          #4

          @SGaist
          It looks like the issue is macOS-specific. Everything works fine on Sequoia 15.7.3. The issue is with Tahoe 26.3.

          1 Reply Last reply
          0
          • SGaistS Offline
            SGaistS Offline
            SGaist
            Lifetime Qt Champion
            wrote last edited by SGaist
            #5

            26.3 might have broken something... Did you already check the bug report system ?

            Interested in AI ? www.idiap.ch
            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

            1 Reply Last reply
            0

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved