Welcome: Determine maximum width for Core::Button text for all states

Button states can have individual text tokens assigned. Depending on the
used fonts and platform-specific renderer, any of these states may have
the highest widts. Consider all of them and use the maximum.

Change-Id: I51caccef9e34c1911c2773b8836dba722ad63c47
Reviewed-by: Alessandro Portale <alessandro.portale@qt.io>
Reviewed-by: <github-actions-qt-creator@cristianadam.eu>
This commit is contained in:
Alessandro Portale
2024-03-06 20:12:40 +01:00
parent 256105d75f
commit c936fc5982

View File

@@ -147,11 +147,16 @@ Button::Button(const QString &text, Role role, QWidget *parent)
QSize Button::minimumSizeHint() const
{
const TextFormat &tf = buttonTF(m_role, WidgetStateHovered);
int maxTextWidth = 0;
for (WidgetState state : {WidgetStateDefault, WidgetStateChecked, WidgetStateHovered} ) {
const TextFormat &tf = buttonTF(m_role, state);
const QFontMetrics fm(tf.font());
const QSize textS = fm.size(Qt::TextShowMnemonic, text());
maxTextWidth = qMax(maxTextWidth, textS.width());
}
const TextFormat &tf = buttonTF(m_role, WidgetStateDefault);
const QMargins margins = contentsMargins();
return {margins.left() + textS.width() + margins.right(),
return {margins.left() + maxTextWidth + margins.right(),
margins.top() + tf.lineHeight() + margins.bottom()};
}