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. macOS dock menu item => Quit - close event does not work
Forum Updated to NodeBB v4.3 + New Features

macOS dock menu item => Quit - close event does not work

Scheduled Pinned Locked Moved Solved General and Desktop
6 Posts 2 Posters 573 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.
  • K Offline
    K Offline
    KejPi
    wrote on last edited by KejPi
    #1

    Hello,
    I have added dock menu item on macOS for my app to control Mute/Unmute. This works great but it turned out that after that change closeEvent can only be triggered by clicking red button (x) on application window. Neither Cmd+Q, Menu->Quit nor right click on dock item->Quit is triggering close event. I can fix Cmd+Q and Menu->Quit by adding menu item explicitly:

    #ifdef Q_OS_MAC
            // this code adds dock right click menu item Mute
            DockMenuHandler * dockMenuPtr = DockMenuHandler::getInstance();
            dockMenuPtr->setMuteItemLabel(tr("Mute"));
            connect(m_muteLabel, &ClickableLabel::toggled, [this](bool checked) {
                DockMenuHandler::getInstance()->setMuteItemLabel(checked ? tr("Unmute") : tr("Mute"));
            });
            connect(dockMenuPtr, &DockMenuHandler::muteItemClicked, m_muteLabel, &ClickableLabel::toggle);
        
    
            // this is to force close event on closing from menu or by Cmd+Q
            QAction * exitAction = new QAction(this);
            exitAction->setMenuRole(QAction::QuitRole);
            QMenuBar * menubar = new QMenuBar(this);
            QMenu * menu = new QMenu(menubar);
            setMenuBar(menubar);
            menubar->addAction(menu->menuAction());
            menu->addAction(exitAction);
            connect(exitAction, &QAction::triggered, this, &QMainWindow::close);
    #endif
    

    But I do not know how to fix closing by right click on dock icon -> Quit. Application actually closes but close event is not triggered and I need that event (or any other event) to save application settings.
    If is comment out code for creating dock menu item everything works as expected but I do not have Mute menu item :-(

    1 Reply Last reply
    0
    • K Offline
      K Offline
      KejPi
      wrote on last edited by
      #3

      I am currently using Qt 6.7.2. It seem to be some interaction with ObjectiveC I have added to implement dock menu item. But I found out that it can be achieved Qt-way. I am not sure if this si correct but it seems to work. Now, I would like to have the same for Windows taskbar ;-) Meanwhile I am marking this topic as solved.

      #ifdef Q_OS_MAC
          QMenu * dockMenu = new QMenu(this);
          m_muteAction = new QAction(tr("Mute"), this);
          dockMenu->addAction(m_muteAction);
          connect(m_muteLabel, &ClickableLabel::toggled, this, [this](bool checked) {
              m_muteAction->setText(checked ? tr("Unmute") : tr("Mute"));
          });
          connect(m_muteAction, &QAction::triggered, m_muteLabel, &ClickableLabel::toggle);
      
          dockMenu->setAsDockMenu();
      #endif
      
      1 Reply Last reply
      0
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by
        #2

        Hi,

        A minimal compilable example showing this issue would help debug it.
        Also, which version of Qt are you using ?
        Which version of macOS ?

        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
        • K Offline
          K Offline
          KejPi
          wrote on last edited by
          #3

          I am currently using Qt 6.7.2. It seem to be some interaction with ObjectiveC I have added to implement dock menu item. But I found out that it can be achieved Qt-way. I am not sure if this si correct but it seems to work. Now, I would like to have the same for Windows taskbar ;-) Meanwhile I am marking this topic as solved.

          #ifdef Q_OS_MAC
              QMenu * dockMenu = new QMenu(this);
              m_muteAction = new QAction(tr("Mute"), this);
              dockMenu->addAction(m_muteAction);
              connect(m_muteLabel, &ClickableLabel::toggled, this, [this](bool checked) {
                  m_muteAction->setText(checked ? tr("Unmute") : tr("Mute"));
              });
              connect(m_muteAction, &QAction::triggered, m_muteLabel, &ClickableLabel::toggle);
          
              dockMenu->setAsDockMenu();
          #endif
          
          1 Reply Last reply
          0
          • K KejPi has marked this topic as solved on
          • SGaistS Offline
            SGaistS Offline
            SGaist
            Lifetime Qt Champion
            wrote on last edited by
            #4

            If memory serves well, there used to be something in the QWindowsExtra module but I don't know if it was ported to Qt 6 yet.

            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
            • K Offline
              K Offline
              KejPi
              wrote on last edited by
              #5

              Exactly, it is available for Qt5 and it seems there is no intention to port it to Qt6. I will try some Windows specific implementation, I hope I will not fall in some trap like i did with ObjectiveC.

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

                You can take inspiration from the code base. No need to reinvent the wheel.

                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