From a09b41bc9fc2bc6c837057f303fd82b19c6f5413 Mon Sep 17 00:00:00 2001 From: Paul Olav Tvete Date: Wed, 22 Jul 2015 13:14:29 +0200 Subject: [PATCH] Fix action shortcuts for QToolButton Reverting change 4db5d3ccd17d448b59db491e2f261892f31fec74, since it introduced two separate regressions: 1. QToolButton would prefer text() over iconText() 2. Actions with a mnemonic would create a shortcut when added to a tool button. There is a fundamental problem here, which is impossible to solve correctly: A menu item with the text "Short&cut" will create a mnemonic, i.e. it will be triggered by pressing 'c' when the menu is open. However, a button with the text "Short&cut" will create a shortcut that is triggered by Alt+C as long as the window has focus. Also, iconText() is used for tool tips, so it should not contain shortcut related '&'s. This patch attempts to find a sensible compromise: 1. If the text is set through QAction::setText(), QToolButton will not create a shortcut, and will not display an underline. 2. Using QAction::setIconText("Short&cut") will create the shortcut. 3. Tooltips will not show any extra '&'s when generated from text(), but when using setIconText() directly, any '&' characters used to create shortcuts will also be visible in the tooltip. Task-number: QTBUG-23396 Task-number: QTBUG-47306 Change-Id: Ieea2fc569807c862ca462349bf91922a4029700f Reviewed-by: Timur Pocheptsov --- src/widgets/kernel/qaction.cpp | 11 +++-------- src/widgets/widgets/qtoolbutton.cpp | 7 ++++++- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/src/widgets/kernel/qaction.cpp b/src/widgets/kernel/qaction.cpp index 9fbcf28aad..4dd10720d6 100644 --- a/src/widgets/kernel/qaction.cpp +++ b/src/widgets/kernel/qaction.cpp @@ -58,14 +58,9 @@ QT_BEGIN_NAMESPACE static QString qt_strippedText(QString s) { s.remove( QString::fromLatin1("...") ); - int i = 0; - while (i < s.size()) { - ++i; - if (s.at(i-1) != QLatin1Char('&')) - continue; - if (i < s.size() && s.at(i) == QLatin1Char('&')) - ++i; - s.remove(i-1,1); + for (int i = 0; i < s.size(); ++i) { + if (s.at(i) == QLatin1Char('&')) + s.remove(i, 1); } return s.trimmed(); } diff --git a/src/widgets/widgets/qtoolbutton.cpp b/src/widgets/widgets/qtoolbutton.cpp index 93f0b60058..69432761e6 100644 --- a/src/widgets/widgets/qtoolbutton.cpp +++ b/src/widgets/widgets/qtoolbutton.cpp @@ -897,7 +897,12 @@ void QToolButton::setDefaultAction(QAction *action) return; if (!actions().contains(action)) addAction(action); - setText(action->text()); + QString buttonText = action->iconText(); + // If iconText() is generated from text(), we need to escape any '&'s so they + // don't turn into shortcuts + if (QActionPrivate::get(action)->iconText.isEmpty()) + buttonText.replace(QLatin1String("&"), QLatin1String("&&")); + setText(buttonText); setIcon(action->icon()); #ifndef QT_NO_TOOLTIP setToolTip(action->toolTip());