Fix action shortcuts for QToolButton

Reverting change 4db5d3ccd1, 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 <Timur.Pocheptsov@digia.com>
bb10
Paul Olav Tvete 2015-07-22 13:14:29 +02:00
parent bfe4bca498
commit a09b41bc9f
2 changed files with 9 additions and 9 deletions

View File

@ -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();
}

View File

@ -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());