From e6deaa1729c419af4e1647530714291325af2d71 Mon Sep 17 00:00:00 2001 From: Fushan Wen Date: Mon, 9 May 2022 16:50:30 +0800 Subject: [PATCH] kstyle: Properly align text in ToolButton with icon MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Sometimes button labels in a ToolButton without icon and a ToolButton with icon are not vertically aligned even if all buttons have the same height. This is caused by double-to-int. The original top padding `(contentsRect.height() - textSize.height()) / 2` can be rounded down, and when the padding is rounded down, so `contentsRect.height() - padding - textSize.height() ≠ padding`. After this change, textRect is calculated based on the remaining space. For example, `contentsRect.height() = 32`, and `textSize.height() = 15`, so the padding is 8. Before this commit, the bottom padding will be `32 - 8 - 15 = 9 ≠ 8`. After this change, the bottom padding is the same as the top padding (8), so the visual glitch is fixed. --- kstyle/breezestyle.cpp | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/kstyle/breezestyle.cpp b/kstyle/breezestyle.cpp index 88d3558d..fd9caab5 100644 --- a/kstyle/breezestyle.cpp +++ b/kstyle/breezestyle.cpp @@ -4647,8 +4647,9 @@ bool Style::drawToolButtonLabelControl(const QStyleOption *option, QPainter *pai iconSize); } - textRect = QRect(QPoint(iconRect.right() + Metrics::ToolButton_ItemSpacing + 1, contentsRect.top() + (contentsRect.height() - textSize.height()) / 2), - textSize); + const int padding = (contentsRect.height() - textSize.height()) / 2; + textRect = QRect(QPoint(iconRect.right() + Metrics::ToolButton_ItemSpacing + 1, contentsRect.top() + padding), + QPoint(contentsRect.right(), contentsRect.bottom() - padding)); // handle right to left layouts iconRect = visualRect(option, iconRect);