From aa8d3f90a440575deef914916299b792105d7209 Mon Sep 17 00:00:00 2001 From: Christian Ehrlicher Date: Sun, 21 Apr 2019 13:09:07 +0200 Subject: QAbstractSpinBox: fix some ui glitches Fix some ui glitches for QAbstractSpinBox: - update geometry when buttons are toggled on/off - calc button size with subControlRect instead hardcoded 20px - when buttons are not shown, don't add the button size in sizeFromContents for common and macOS style Fixes: QTBUG-39713 Fixes: QTBUG-75303 Task-number: QTBUG-67126 Change-Id: Ibf330c76deb16358a481bba6bd429fff6a5d57ae Reviewed-by: Richard Moe Gustavsen --- src/widgets/styles/qcommonstyle.cpp | 3 ++- src/widgets/styles/qstylesheetstyle.cpp | 30 ++++++++++++++++++------------ 2 files changed, 20 insertions(+), 13 deletions(-) (limited to 'src/widgets/styles') diff --git a/src/widgets/styles/qcommonstyle.cpp b/src/widgets/styles/qcommonstyle.cpp index a3461d8848..35b630cde2 100644 --- a/src/widgets/styles/qcommonstyle.cpp +++ b/src/widgets/styles/qcommonstyle.cpp @@ -5029,7 +5029,8 @@ QSize QCommonStyle::sizeFromContents(ContentsType ct, const QStyleOption *opt, case CT_SpinBox: if (const QStyleOptionSpinBox *vopt = qstyleoption_cast(opt)) { // Add button + frame widths - const int buttonWidth = (vopt->subControls & (QStyle::SC_SpinBoxUp | QStyle::SC_SpinBoxDown)) != 0 ? 20 : 0; + const bool hasButtons = (vopt->buttonSymbols != QAbstractSpinBox::NoButtons); + const int buttonWidth = hasButtons ? proxy()->subControlRect(CC_SpinBox, vopt, SC_SpinBoxUp, widget).width() : 0; const int fw = vopt->frame ? proxy()->pixelMetric(PM_SpinBoxFrameWidth, vopt, widget) : 0; sz += QSize(buttonWidth + 2*fw, 2*fw); } diff --git a/src/widgets/styles/qstylesheetstyle.cpp b/src/widgets/styles/qstylesheetstyle.cpp index 2118bad9d6..84299263d7 100644 --- a/src/widgets/styles/qstylesheetstyle.cpp +++ b/src/widgets/styles/qstylesheetstyle.cpp @@ -4987,17 +4987,19 @@ QSize QStyleSheetStyle::sizeFromContents(ContentsType ct, const QStyleOption *op switch (ct) { #if QT_CONFIG(spinbox) - case CT_SpinBox: // ### hopelessly broken QAbstractSpinBox (part 1) + case CT_SpinBox: if (const QStyleOptionSpinBox *spinbox = qstyleoption_cast(opt)) { - // Add some space for the up/down buttons - QRenderRule subRule = renderRule(w, opt, PseudoElement_SpinBoxUpButton); - if (subRule.hasDrawable()) { - QRect r = positionRect(w, rule, subRule, PseudoElement_SpinBoxUpButton, - opt->rect, opt->direction); - sz += QSize(r.width(), 0); - } else { - QSize defaultUpSize = defaultSize(w, subRule.size(), spinbox->rect, PseudoElement_SpinBoxUpButton); - sz += QSize(defaultUpSize.width(), 0); + if (spinbox->buttonSymbols != QAbstractSpinBox::NoButtons) { + // Add some space for the up/down buttons + QRenderRule subRule = renderRule(w, opt, PseudoElement_SpinBoxUpButton); + if (subRule.hasDrawable()) { + QRect r = positionRect(w, rule, subRule, PseudoElement_SpinBoxUpButton, + opt->rect, opt->direction); + sz.rwidth() += r.width(); + } else { + QSize defaultUpSize = defaultSize(w, subRule.size(), spinbox->rect, PseudoElement_SpinBoxUpButton); + sz.rwidth() += defaultUpSize.width(); + } } if (rule.hasBox() || rule.hasBorder() || !rule.hasNativeBorder()) sz = rule.boxSize(sz); @@ -5498,8 +5500,12 @@ QRect QStyleSheetStyle::subControlRect(ComplexControl cc, const QStyleOptionComp : Qt::Alignment(Qt::AlignRight); downAlign = resolveAlignment(opt->direction, downAlign); - int upSize = subControlRect(CC_SpinBox, opt, SC_SpinBoxUp, w).width(); - int downSize = subControlRect(CC_SpinBox, opt, SC_SpinBoxDown, w).width(); + const bool hasButtons = (spin->buttonSymbols != QAbstractSpinBox::NoButtons); + const int upSize = hasButtons + ? subControlRect(CC_SpinBox, opt, SC_SpinBoxUp, w).width() : 0; + const int downSize = hasButtons + ? subControlRect(CC_SpinBox, opt, SC_SpinBoxDown, w).width() : 0; + int widestL = qMax((upAlign & Qt::AlignLeft) ? upSize : 0, (downAlign & Qt::AlignLeft) ? downSize : 0); int widestR = qMax((upAlign & Qt::AlignRight) ? upSize : 0, -- cgit v1.2.3