macOS: delay initialization of small font

During construction of a QStyle, QApplication might not yet be
initialized, e.g. when calling QApplication::setStyle("macOS") before
constructing QApplication. In that case, we cannot access the platform
theme.

We don't just want to skip initializing the small font either though.
Store the smallSystemFont as a std::optional so that we can initialize
it once, when the first widget gets polished.

As a drive-by, remove the unused miniSystemFont variable.

Fixes: QTBUG-108047
Change-Id: Id750770a563611fdbc6c7031fe102a99ea692be7
Reviewed-by: Samuel Gaist <samuel.gaist@idiap.ch>
(cherry picked from commit 2bdc027f5c6be9c551d40c80e510b19b6e2939a7)
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
This commit is contained in:
Volker Hilsheimer 2022-11-08 18:42:56 +01:00 committed by Qt Cherry-pick Bot
parent f71c4df4b2
commit 32af1fe7d3
2 changed files with 14 additions and 9 deletions

View File

@ -1794,10 +1794,6 @@ QRectF QMacStylePrivate::comboboxEditBounds(const QRectF &outerBounds, const Coc
QMacStylePrivate::QMacStylePrivate()
: backingStoreNSView(nil)
{
if (auto *ssf = QGuiApplicationPrivate::platformTheme()->font(QPlatformTheme::SmallFont))
smallSystemFont = *ssf;
if (auto *msf = QGuiApplicationPrivate::platformTheme()->font(QPlatformTheme::MiniFont))
miniSystemFont = *msf;
}
QMacStylePrivate::~QMacStylePrivate()
@ -2092,6 +2088,14 @@ void QMacStyle::unpolish(QApplication *)
void QMacStyle::polish(QWidget* w)
{
Q_D(QMacStyle);
if (!d->smallSystemFont && QGuiApplicationPrivate::platformTheme()) {
if (auto *ssf = QGuiApplicationPrivate::platformTheme()->font(QPlatformTheme::SmallFont))
d->smallSystemFont = *ssf;
else
d->smallSystemFont = QFont();
}
if (false
#if QT_CONFIG(menu)
|| qobject_cast<QMenu*>(w)
@ -5689,8 +5693,8 @@ void QMacStyle::drawComplexControl(ComplexControl cc, const QStyleOptionComplex
const bool rtl = groupBox.direction == Qt::RightToLeft;
const int alignment = Qt::TextHideMnemonic | (rtl ? Qt::AlignRight : Qt::AlignLeft);
const QFont savedFont = p->font();
if (!flat)
p->setFont(d->smallSystemFont);
if (!flat && d->smallSystemFont)
p->setFont(*d->smallSystemFont);
proxy()->drawItemText(p, rect, alignment, groupBox.palette, groupBox.state & State_Enabled, groupBox.text, QPalette::WindowText);
if (!flat)
p->setFont(savedFont);
@ -6054,7 +6058,9 @@ QRect QMacStyle::subControlRect(ComplexControl cc, const QStyleOptionComplex *op
const int margin = flat || hasNoText ? 0 : 9;
ret = groupBox->rect.adjusted(margin, 0, -margin, 0);
const QFontMetricsF fm = flat || fontIsSet ? QFontMetricsF(groupBox->fontMetrics) : QFontMetricsF(d->smallSystemFont);
const QFontMetricsF fm = flat || fontIsSet || !d->smallSystemFont
? QFontMetricsF(groupBox->fontMetrics)
: QFontMetricsF(*d->smallSystemFont);
const QSizeF s = fm.size(Qt::AlignHCenter | Qt::AlignVCenter, qt_mac_removeMnemonics(groupBox->text), 0, nullptr);
const int tw = qCeil(s.width());
const int h = qCeil(fm.height());

View File

@ -262,8 +262,7 @@ public:
mutable QHash<CocoaControl, NSView *> cocoaControls;
mutable QHash<CocoaControl, NSCell *> cocoaCells;
QFont smallSystemFont;
QFont miniSystemFont;
std::optional<QFont> smallSystemFont;
QMacKeyValueObserver appearanceObserver;
};