macOS dock menu item => Quit - close event does not work
-
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); #endifBut 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 :-( -
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 -
Hi,
A minimal compilable example showing this issue would help debug it.
Also, which version of Qt are you using ?
Which version of macOS ? -
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 -
K KejPi has marked this topic as solved on
-
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.
-
You can take inspiration from the code base. No need to reinvent the wheel.