From 9c84620d4b610e07b4245c295c7234294f2067ce Mon Sep 17 00:00:00 2001 From: Christian Ehrlicher Date: Fri, 17 Nov 2023 21:21:57 +0100 Subject: [PATCH] QPushButton: fix QPushButton with icon in rtl mode In rtl mode the text was not aligned right so a gap between the text and the icon occurred. This was due to the fact that the textFlag given to qt_format_text() was modified based on the application wide rtl/ltr mode and so our Qt::RightToLeft was reverted to Qt::LeftToRight. Therefore only set the textFlag to Qt::AlignRight when the button layout direction does not match the global one. Fixes: QTBUG-64373 Change-Id: I283a99de8f59bc3db53c4150f417ffef9ff68db7 Reviewed-by: Volker Hilsheimer Reviewed-by: Axel Spoerl (cherry picked from commit b03557287fef291fc0a79ba2dc7c0f8bb3a0547b) Reviewed-by: Qt Cherry-pick Bot --- src/widgets/styles/qcommonstyle.cpp | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/src/widgets/styles/qcommonstyle.cpp b/src/widgets/styles/qcommonstyle.cpp index b32198c5e6f..f5dd5396505 100644 --- a/src/widgets/styles/qcommonstyle.cpp +++ b/src/widgets/styles/qcommonstyle.cpp @@ -1396,13 +1396,16 @@ void QCommonStyle::drawControl(ControlElement element, const QStyleOption *opt, iconRect = visualRect(button->direction, textRect, iconRect); - if (button->direction == Qt::RightToLeft) { - tf |= Qt::AlignRight; + if (button->direction == Qt::RightToLeft) textRect.setRight(iconRect.left() - iconSpacing / 2); - } else { - tf |= Qt::AlignLeft; //left align, we adjust the text-rect instead + else textRect.setLeft(iconRect.left() + iconRect.width() + iconSpacing / 2); - } + + // qt_format_text reverses again when painter->layoutDirection is also RightToLeft + if (p->layoutDirection() == button->direction) + tf |= Qt::AlignLeft; + else + tf |= Qt::AlignRight; if (button->state & (State_On | State_Sunken)) iconRect.translate(proxy()->pixelMetric(PM_ButtonShiftHorizontal, opt, widget),