diff options
author | Jake Petroules <jake.petroules@qt.io> | 2017-03-10 16:55:04 -0800 |
---|---|---|
committer | Jake Petroules <jake.petroules@qt.io> | 2017-03-14 05:29:22 +0000 |
commit | c2c3452ba5b4c32d0c2d5df9193bf89986623ab5 (patch) | |
tree | 66b567681989e166bc429f477ae7f13f9c844f75 /src/widgets/kernel/qaction.cpp | |
parent | b6e3fd8ced2ae3629b73b78bf436d1a3ac4bccda (diff) |
Add new API to control whether shortcuts are shown in context menus
Shortcuts are universally not shown on macOS or mobile platforms, making
applications look very obviously out of place.
Windows and GNOME desktop environments almost never use them.
Only KDE appears to do so commonly; default accordingly.
Task-number: QTBUG-49435
Change-Id: Ieac4cee57b15a02be5258f3d07749af6316af62b
Reviewed-by: Gabriel de Dietrich <gabriel.dedietrich@qt.io>
Diffstat (limited to 'src/widgets/kernel/qaction.cpp')
-rw-r--r-- | src/widgets/kernel/qaction.cpp | 42 |
1 files changed, 42 insertions, 0 deletions
diff --git a/src/widgets/kernel/qaction.cpp b/src/widgets/kernel/qaction.cpp index 4582a55394..ec7dc34d4d 100644 --- a/src/widgets/kernel/qaction.cpp +++ b/src/widgets/kernel/qaction.cpp @@ -45,6 +45,7 @@ #include "qapplication.h" #include "qevent.h" #include "qlist.h" +#include "qstylehints.h" #include <private/qshortcutmap_p.h> #include <private/qapplication_p.h> #include <private/qmenu_p.h> @@ -75,6 +76,7 @@ static QString qt_strippedText(QString s) QActionPrivate::QActionPrivate() : group(0), enabled(1), forceDisabled(0), visible(1), forceInvisible(0), checkable(0), checked(0), separator(0), fontSet(false), iconVisibleInMenu(-1), + shortcutVisibleInContextMenu(-1), menuRole(QAction::TextHeuristicRole), priority(QAction::NormalPriority) { @@ -1292,6 +1294,46 @@ bool QAction::isIconVisibleInMenu() const return d->iconVisibleInMenu; } +/*! + \property QAction::shortcutVisibleInContextMenu + \brief Whether or not an action should show a shortcut in a context menu + \since 5.10 + + In some applications, it may make sense to have actions with shortcuts in + context menus. If true, the shortcut (if valid) is shown when the action is + shown via a context menu, when it is false, it is not shown. + + The default is to follow whether the Qt::AA_DontShowShortcutsInContextMenus attribute + is set for the application, falling back to the widget style hint. + Explicitly setting this property overrides the presence (or abscence) of the attribute. + + \sa QAction::shortcut, QCoreApplication::setAttribute() +*/ +void QAction::setShortcutVisibleInContextMenu(bool visible) +{ + Q_D(QAction); + if (d->shortcutVisibleInContextMenu == -1 || visible != bool(d->shortcutVisibleInContextMenu)) { + int oldValue = d->shortcutVisibleInContextMenu; + d->shortcutVisibleInContextMenu = visible; + // Only send data changed if we really need to. + if (oldValue != -1 + || visible == !QApplication::instance()->testAttribute(Qt::AA_DontShowShortcutsInContextMenus)) { + d->sendDataChanged(); + } + } +} + +bool QAction::isShortcutVisibleInContextMenu() const +{ + Q_D(const QAction); + if (d->shortcutVisibleInContextMenu == -1) { + if (QApplication::instance()->testAttribute(Qt::AA_DontShowIconsInMenus)) + return false; + return qApp->styleHints()->showShortcutsInContextMenus(); + } + return d->shortcutVisibleInContextMenu; +} + #ifndef QT_NO_DEBUG_STREAM Q_WIDGETS_EXPORT QDebug operator<<(QDebug d, const QAction *action) { |