QSystemTrayIcon: Allow resetting context menu

Fix logic in QSystemTrayIconPrivate::updateMenu_sys() to allow resetting
the tray icon menu. Now we correctly handle `nullptr` menu, and update
the underlying QPlatformSystemTrayIcon instance accordingly.

Also we bail out from QSystemTrayIcon::setContextMenu() early if the
menu is the same.

Fixes: QTBUG-119068
Pick-to: 6.5
Change-Id: I704b40dfb1a3046331aef65417655411bf3a41a0
Reviewed-by: Tor Arne Vestbø <tor.arne.vestbo@qt.io>
(cherry picked from commit c1d389a132d291fb404bef11de3c1b45dbfa2afc)
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
This commit is contained in:
Vladimir Belyavsky 2023-11-12 20:06:19 +03:00 committed by Qt Cherry-pick Bot
parent b44828959e
commit 8fcd3b1b1b
2 changed files with 12 additions and 4 deletions

View File

@ -162,9 +162,13 @@ void QSystemTrayIcon::setContextMenu(QMenu *menu)
{
Q_D(QSystemTrayIcon);
QMenu *oldMenu = d->menu.data();
if (oldMenu == menu)
return;
d->menu = menu;
d->updateMenu_sys();
if (oldMenu != menu && d->qpa_sys) {
if (d->qpa_sys) {
// Show the QMenu-based menu for QPA plugins that do not provide native menus
if (oldMenu && !oldMenu->platformMenu())
QObject::disconnect(d->qpa_sys, &QPlatformSystemTrayIcon::contextMenuRequested, menu, nullptr);

View File

@ -59,9 +59,13 @@ void QSystemTrayIconPrivate::updateIcon_sys()
void QSystemTrayIconPrivate::updateMenu_sys()
{
#if QT_CONFIG(menu)
if (qpa_sys && menu) {
addPlatformMenu(menu);
qpa_sys->updateMenu(menu->platformMenu());
if (qpa_sys) {
if (menu) {
addPlatformMenu(menu);
qpa_sys->updateMenu(menu->platformMenu());
} else {
qpa_sys->updateMenu(nullptr);
}
}
#endif
}