QToolButton: fix handling multi-line texts

The patch to elide the QToolButton text when there is not enough space
introduced a regression with multi-line text.
Fix it by using the newly introduced common function to elide multi-line
text.

Fixes: QTBUG-72226
Change-Id: I066ebbd2f360add93406cc29bb4bbbebf599ba42
Reviewed-by: Samuel Gaist <samuel.gaist@idiap.ch>
Reviewed-by: Richard Moe Gustavsen <richard.gustavsen@qt.io>
This commit is contained in:
Christian Ehrlicher 2019-01-27 21:15:52 +01:00
parent a7ba79553c
commit 7024090e1d
2 changed files with 25 additions and 2 deletions

View File

@ -1157,6 +1157,25 @@ void QCommonStylePrivate::viewItemLayout(const QStyleOptionViewItem *opt, QRect
}
#endif // QT_CONFIG(itemviews)
#if QT_CONFIG(toolbutton)
QString QCommonStylePrivate::toolButtonElideText(const QStyleOptionToolButton *option,
const QRect &textRect, int flags) const
{
if (option->fontMetrics.horizontalAdvance(option->text) <= textRect.width())
return option->text;
QString text = option->text;
text.replace('\n', QChar::LineSeparator);
QTextOption textOption;
textOption.setWrapMode(QTextOption::ManualWrap);
textOption.setTextDirection(option->direction);
return calculateElidedText(text, textOption,
option->font, textRect, Qt::AlignTop,
Qt::ElideMiddle, flags,
false, nullptr);
}
#endif // QT_CONFIG(toolbutton)
#if QT_CONFIG(tabbar)
/*! \internal
@ -1705,8 +1724,7 @@ void QCommonStyle::drawControl(ControlElement element, const QStyleOption *opt,
alignment |= Qt::AlignLeft | Qt::AlignVCenter;
}
tr.translate(shiftX, shiftY);
const QString text = toolbutton->fontMetrics.elidedText(toolbutton->text, Qt::ElideMiddle,
tr.width(), alignment);
const QString text = d->toolButtonElideText(toolbutton, tr, alignment);
proxy()->drawItemText(p, QStyle::visualRect(opt->direction, rect, tr), alignment, toolbutton->palette,
toolbutton->state & State_Enabled, text,
QPalette::ButtonText);

View File

@ -115,6 +115,11 @@ public:
&& option.viewItemPosition == cachedOption->viewItemPosition);
}
#endif
#if QT_CONFIG(toolbutton)
QString toolButtonElideText(const QStyleOptionToolButton *toolbutton,
const QRect &textRect, int flags) const;
#endif
mutable QIcon tabBarcloseButtonIcon;
#if QT_CONFIG(tabbar)
void tabLayout(const QStyleOptionTab *opt, const QWidget *widget, QRect *textRect, QRect *pixmapRect) const;