From 3a70e669ef1f236fe564e4ffcb5a9e1ebbff3523 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tor=20Arne=20Vestb=C3=B8?= Date: Wed, 12 Jun 2024 18:52:12 +0200 Subject: [PATCH] Determine Qt::AA_DontShowIconsInMenus default value based on platform Some platforms, such as macOS, do not natively have icons in menus, except in a few specific cases, so the default for AA_DontShowIconsInMenus and in turn QAction.iconVisibleInMenu should be to not show the icon. QActions with iconVisibleInMenu explicitly set to true will still show the icon, even if the user does not override AA_DontShowIconsInMenus. [ChangeLog][QtGui] The default value of Qt::AA_DontShowIconsInMenus is now determined based on the platform. On macOS icons will not show by default. To override, use QAction.iconVisibleInMenu for individual menu actions, or set Qt::AA_DontShowIconsInMenus to false. Pick-to: 6.7 Change-Id: Ie4fb3e6618486233d64a7818be19a42ed9a2a852 Reviewed-by: Volker Hilsheimer (cherry picked from commit d671e1af3b736ee7d866323246fc2190fc5e076a) Reviewed-by: Qt Cherry-pick Bot --- src/corelib/global/qnamespace.qdoc | 5 ++++- src/gui/kernel/qaction.cpp | 6 ++++-- src/gui/kernel/qguiapplication.cpp | 5 +++++ src/gui/kernel/qplatformtheme.cpp | 2 ++ src/gui/kernel/qplatformtheme.h | 1 + src/plugins/platforms/cocoa/qcocoatheme.mm | 2 ++ 6 files changed, 18 insertions(+), 3 deletions(-) diff --git a/src/corelib/global/qnamespace.qdoc b/src/corelib/global/qnamespace.qdoc index a393b30f892..64da69c0ac1 100644 --- a/src/corelib/global/qnamespace.qdoc +++ b/src/corelib/global/qnamespace.qdoc @@ -96,7 +96,10 @@ \value AA_DontShowIconsInMenus Actions with the Icon property won't be shown in any menus unless specifically set by the - QAction::iconVisibleInMenu property. + QAction::iconVisibleInMenu property. The default value of this + attribute depends on the platform. To override the default + behavior, set the attribute after QGuiApplication has been + instantiated. Menus that are currently open or menus already created in the native \macos menubar \e{may not} pick up a change in this attribute. Changes in the QAction::iconVisibleInMenu property will always be picked up. diff --git a/src/gui/kernel/qaction.cpp b/src/gui/kernel/qaction.cpp index 35f7ec12bfd..1768cd8aa1a 100644 --- a/src/gui/kernel/qaction.cpp +++ b/src/gui/kernel/qaction.cpp @@ -538,8 +538,10 @@ QList QAction::associatedObjects() const \brief the action's icon In toolbars, the icon is used as the tool button icon; in menus, - it is displayed to the left of the menu text. There is no default - icon. + it is displayed to the left of the menu text, as long as + QAction::iconVisibleInMenu returns \c true. + + There is no default icon. If a null icon (QIcon::isNull()) is passed into this function, the icon of the action is cleared. diff --git a/src/gui/kernel/qguiapplication.cpp b/src/gui/kernel/qguiapplication.cpp index 9ef61ef4fa8..f11193055e1 100644 --- a/src/gui/kernel/qguiapplication.cpp +++ b/src/gui/kernel/qguiapplication.cpp @@ -1396,6 +1396,11 @@ static void init_platform(const QString &pluginNamesWithArguments, const QString fontSmoothingGamma = platformIntegration->styleHint(QPlatformIntegration::FontSmoothingGamma).toReal(); QCoreApplication::setAttribute(Qt::AA_DontShowShortcutsInContextMenus, !QGuiApplication::styleHints()->showShortcutsInContextMenus()); + + if (const auto *platformTheme = QGuiApplicationPrivate::platformTheme()) { + QCoreApplication::setAttribute(Qt::AA_DontShowIconsInMenus, + !platformTheme->themeHint(QPlatformTheme::ShowIconsInMenus).toBool()); + } } static void init_plugins(const QList &pluginList) diff --git a/src/gui/kernel/qplatformtheme.cpp b/src/gui/kernel/qplatformtheme.cpp index 3d1319615ed..f298cadae6b 100644 --- a/src/gui/kernel/qplatformtheme.cpp +++ b/src/gui/kernel/qplatformtheme.cpp @@ -652,6 +652,8 @@ QVariant QPlatformTheme::defaultThemeHint(ThemeHint hint) return QVariant(QSize(16, 16)); case UnderlineShortcut: return true; + case ShowIconsInMenus: + return true; } return QVariant(); diff --git a/src/gui/kernel/qplatformtheme.h b/src/gui/kernel/qplatformtheme.h index d007a196757..3ce094d85b3 100644 --- a/src/gui/kernel/qplatformtheme.h +++ b/src/gui/kernel/qplatformtheme.h @@ -97,6 +97,7 @@ public: MouseCursorTheme, MouseCursorSize, UnderlineShortcut, + ShowIconsInMenus, }; Q_ENUM(ThemeHint) diff --git a/src/plugins/platforms/cocoa/qcocoatheme.mm b/src/plugins/platforms/cocoa/qcocoatheme.mm index f3f3e05b502..1623b12be63 100644 --- a/src/plugins/platforms/cocoa/qcocoatheme.mm +++ b/src/plugins/platforms/cocoa/qcocoatheme.mm @@ -464,6 +464,8 @@ QVariant QCocoaTheme::themeHint(ThemeHint hint) const return NSEvent.keyRepeatDelay * 1000; case QPlatformTheme::KeyboardAutoRepeatRate: return 1.0 / NSEvent.keyRepeatInterval; + case QPlatformTheme::ShowIconsInMenus: + return false; default: break; }