summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorVolker Hilsheimer <volker.hilsheimer@qt.io>2021-06-04 23:03:43 +0200
committerQt Cherry-pick Bot <cherrypick_bot@qt-project.org>2021-06-08 19:10:06 +0000
commit294b50e1f5c388f666d10febe3fcc024d1d13c24 (patch)
treee0aa4f7d9b27af908715f2e51c2864d0b4771e6e /src
parentc58d691938803148b98f820b5c1a8f4742966e92 (diff)
Calculate space requirement of menu items based on widest elements
Since icon, checkmark, text, and shortcut are each rendered aligned within their own column, we need to take the widest of each element into account when calculating the size requirement of each item. Otherwise an item with very long text but no icon will get enough space for the text, but no space for the icon, resulting in the text running over the shortcut or edge of the menu. Fortunately, QStyleOptionMenuItem provides us with the necessary information. Fixes: QTBUG-86754 Change-Id: I0cf0e9adfe480d1004106e7475e498e718bf027b Reviewed-by: Qt CI Bot <qt_ci_bot@qt-project.org> Reviewed-by: Eirik Aavitsland <eirik.aavitsland@qt.io> (cherry picked from commit c8cb2409477bdc3bc41d6bbdfc5055441cda084f) Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
Diffstat (limited to 'src')
-rw-r--r--src/widgets/styles/qstylesheetstyle.cpp9
1 files changed, 5 insertions, 4 deletions
diff --git a/src/widgets/styles/qstylesheetstyle.cpp b/src/widgets/styles/qstylesheetstyle.cpp
index 9a80347d7e..5e3d342e88 100644
--- a/src/widgets/styles/qstylesheetstyle.cpp
+++ b/src/widgets/styles/qstylesheetstyle.cpp
@@ -5185,18 +5185,19 @@ QSize QStyleSheetStyle::sizeFromContents(ContentsType ct, const QStyleOption *op
QSize sz(csz);
if (mi->text.contains(QLatin1Char('\t')))
sz.rwidth() += 12; //as in QCommonStyle
- bool checkable = mi->checkType != QStyleOptionMenuItem::NotCheckable;
if (!mi->icon.isNull()) {
const int pmSmall = pixelMetric(PM_SmallIconSize);
const QSize pmSize = mi->icon.actualSize(QSize(pmSmall, pmSmall));
- sz.rwidth() += pmSize.width() + 4;
- } else if (checkable) {
+ sz.rwidth() += std::max(mi->maxIconWidth, pmSize.width()) + 4;
+ } else if (mi->menuHasCheckableItems) {
QRenderRule subSubRule = renderRule(w, opt, PseudoElement_MenuCheckMark);
QRect checkmarkRect = positionRect(w, subRule, subSubRule, PseudoElement_MenuCheckMark, opt->rect, opt->direction);
sz.rwidth() += std::max(mi->maxIconWidth, checkmarkRect.width()) + 4;
+ } else {
+ sz.rwidth() += mi->maxIconWidth;
}
if (subRule.hasFont) {
- QFontMetrics fm(subRule.font);
+ QFontMetrics fm(subRule.font.resolve(mi->font));
const QRect r = fm.boundingRect(QRect(), Qt::TextSingleLine | Qt::TextShowMnemonic, mi->text);
sz = sz.expandedTo(r.size());
}