From c00f52e821a3a982e91d9c4515c3d4d944305a16 Mon Sep 17 00:00:00 2001 From: ChunLin Wang Date: Mon, 11 Jan 2021 13:14:43 +0800 Subject: Fix QApplication::font returns the font unstable according to the object If a default font was not registered for the widget's class, it returns the default font of its nearest registered superclass. Fixes: QTBUG-89910 Change-Id: I6e6b2c6a0044462f84db9f76a03be0c6cfaaae8e Reviewed-by: Volker Hilsheimer (cherry picked from commit dafd26acbe7b08f5ddfe60432fe0e49422ac085c) Reviewed-by: Qt Cherry-pick Bot --- .../kernel/qapplication/tst_qapplication.cpp | 42 ++++++++++++++++++++++ 1 file changed, 42 insertions(+) (limited to 'tests') diff --git a/tests/auto/widgets/kernel/qapplication/tst_qapplication.cpp b/tests/auto/widgets/kernel/qapplication/tst_qapplication.cpp index 6763d3b6a6..750810724c 100644 --- a/tests/auto/widgets/kernel/qapplication/tst_qapplication.cpp +++ b/tests/auto/widgets/kernel/qapplication/tst_qapplication.cpp @@ -54,6 +54,7 @@ #include #include #include +#include #include #include #include @@ -90,6 +91,7 @@ private slots: void setFont_data(); void setFont(); + void setFontForClass(); void args_data(); void args(); @@ -421,6 +423,46 @@ void tst_QApplication::setFont() QCOMPARE( app.font(), font ); } +class tstHeaderView : public QHeaderView +{ + Q_OBJECT +public: + explicit tstHeaderView(Qt::Orientation orientation, QWidget *parent = nullptr) + : QHeaderView(orientation, parent) + {} +}; +class tstFrame : public QFrame { Q_OBJECT }; +class tstWidget : public QWidget { Q_OBJECT }; + +void tst_QApplication::setFontForClass() +{ + // QTBUG-89910 + // If a default font was not registered for the widget's class, + // it returns the default font of its nearest registered superclass. + int argc = 0; + QApplication app(argc, nullptr); + + QFont font; + int pointSize = 10; + const QByteArrayList classNames{"QHeaderView", "QAbstractItemView", "QAbstractScrollView", "QFrame", "QWidget", "QObject"}; + for (auto className : classNames) { + font.setPointSizeF(pointSize++); + app.setFont(font, className.constData()); + } + + tstHeaderView headView(Qt::Horizontal); + tstFrame frame; + tstWidget widget; + + QFont headViewFont = QApplication::font(&headView); + QFont frameFont = QApplication::font(&frame); + QFont widgetFont = QApplication::font(&widget); + + QCOMPARE(headViewFont.pointSize(), QApplication::font("QHeaderView").pointSize()); + QCOMPARE(frameFont.pointSize(), QApplication::font("QFrame").pointSize()); + QCOMPARE(widgetFont.pointSize(), QApplication::font("QWidget").pointSize()); +} + void tst_QApplication::args_data() { QTest::addColumn("argc_in"); -- cgit v1.2.3