kstyle: Properly align text in ToolButton with icon

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.
wilder
Fushan Wen 4 years ago
parent a907a4aa17
commit e6deaa1729
No known key found for this signature in database
GPG Key ID: 2E48D1487C91DCAA
  1. 5
      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);

Loading…
Cancel
Save