diff options
author | Steve Mokris <smokris@softpixel.com> | 2018-02-13 12:21:38 -0500 |
---|---|---|
committer | Steve Mokris <smokris@softpixel.com> | 2018-03-01 23:58:05 +0000 |
commit | aa1bc47942e2062dc7fad3e139bb4ceecc74f947 (patch) | |
tree | d999fab4c39374b0ca3864cae3fdaa0918e6d738 | |
parent | 529cb7217c0162c31ef6f309730e93612b9798d3 (diff) |
Show the checkmark when rendering a QAction in a QMenu using QSS
Task-number: QTBUG-66380
Change-Id: If7376430cfa021cb6eca1cbbcea6b6d0e01802bf
Reviewed-by: Richard Moe Gustavsen <richard.gustavsen@qt.io>
-rw-r--r-- | src/widgets/styles/qstylesheetstyle.cpp | 37 |
1 files changed, 30 insertions, 7 deletions
diff --git a/src/widgets/styles/qstylesheetstyle.cpp b/src/widgets/styles/qstylesheetstyle.cpp index 295f47c310..36e5b03c0d 100644 --- a/src/widgets/styles/qstylesheetstyle.cpp +++ b/src/widgets/styles/qstylesheetstyle.cpp @@ -3671,6 +3671,17 @@ void QStyleSheetStyle::drawControl(ControlElement ce, const QStyleOption *opt, Q bool dis = !(opt->state & QStyle::State_Enabled), act = opt->state & QStyle::State_Selected; + int checkableOffset = 0; + if (checkable) { + QRenderRule subSubRule = renderRule(w, opt, PseudoElement_MenuCheckMark); + QStyleOptionMenuItem newMi = mi; + newMi.rect = positionRect(w, subRule, subSubRule, PseudoElement_MenuCheckMark, opt->rect, opt->direction); + checkableOffset = newMi.rect.width(); + if (subSubRule.hasDrawable() || checked) + drawPrimitive(PE_IndicatorMenuCheckMark, &newMi, p, w); + } + + int iconOffset = 0; if (!mi.icon.isNull()) { QIcon::Mode mode = dis ? QIcon::Disabled : QIcon::Normal; if (act && !dis) @@ -3690,20 +3701,22 @@ void QStyleSheetStyle::drawControl(ControlElement ce, const QStyleOption *opt, Q iconRule.geo->height = pixh; } QRect iconRect = positionRect(w, subRule, iconRule, PseudoElement_MenuIcon, opt->rect, opt->direction); + if (opt->direction == Qt::LeftToRight) + iconRect.moveLeft(iconRect.left() + checkableOffset); + else + iconRect.moveRight(iconRect.right() - checkableOffset); iconRule.drawRule(p, iconRect); QRect pmr(0, 0, pixw, pixh); pmr.moveCenter(iconRect.center()); p->drawPixmap(pmr.topLeft(), pixmap); - } else if (checkable) { - QRenderRule subSubRule = renderRule(w, opt, PseudoElement_MenuCheckMark); - if (subSubRule.hasDrawable() || checked) { - QStyleOptionMenuItem newMi = mi; - newMi.rect = positionRect(w, subRule, subSubRule, PseudoElement_MenuCheckMark, opt->rect, opt->direction); - drawPrimitive(PE_IndicatorMenuCheckMark, &newMi, p, w); - } + iconOffset = iconRule.geo->width; } QRect textRect = subRule.contentsRect(opt->rect); + if (opt->direction == Qt::LeftToRight) + textRect.setLeft(textRect.left() + checkableOffset + iconOffset); + else + textRect.setRight(textRect.right() - checkableOffset - iconOffset); textRect.setWidth(textRect.width() - mi.tabWidth); QStringRef s(&mi.text); p->setPen(mi.palette.buttonText().color()); @@ -5045,6 +5058,16 @@ QSize QStyleSheetStyle::sizeFromContents(ContentsType ct, const QStyleOption *op int width = csz.width(); if (mi->text.contains(QLatin1Char('\t'))) width += 12; //as in QCommonStyle + bool checkable = mi->checkType != QStyleOptionMenuItem::NotCheckable; + if (checkable) { + QRenderRule subSubRule = renderRule(w, opt, PseudoElement_MenuCheckMark); + QRect checkmarkRect = positionRect(w, subRule, subSubRule, PseudoElement_MenuCheckMark, opt->rect, opt->direction); + width += checkmarkRect.width(); + } + if (!mi->icon.isNull()) { + QPixmap pixmap = mi->icon.pixmap(pixelMetric(PM_SmallIconSize)); + width += pixmap.width(); + } return subRule.boxSize(subRule.adjustSize(QSize(width, csz.height()))); } } |