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 <volker.hilsheimer@qt.io> (cherry picked from commit dafd26acbe7b08f5ddfe60432fe0e49422ac085c) Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
This commit is contained in:
parent
5b3dbd38ca
commit
c00f52e821
@ -1270,9 +1270,11 @@ QFont QApplication::font()
|
||||
/*!
|
||||
\overload
|
||||
|
||||
Returns the default font for the \a widget.
|
||||
Returns the default font for the \a widget. If a default font was not
|
||||
registered for the \a{widget}'s class, it returns the default font of
|
||||
its nearest registered superclass.
|
||||
|
||||
\sa fontMetrics(), QWidget::setFont()
|
||||
\sa fontMetrics(), setFont(), QWidget::setFont()
|
||||
*/
|
||||
|
||||
QFont QApplication::font(const QWidget *widget)
|
||||
@ -1290,14 +1292,16 @@ QFont QApplication::font(const QWidget *widget)
|
||||
return hash->value(QByteArrayLiteral("QMiniFont"));
|
||||
}
|
||||
#endif
|
||||
FontHashConstIt it = hash->constFind(widget->metaObject()->className());
|
||||
// Return the font for the nearest registered superclass
|
||||
const QMetaObject *metaObj = widget->metaObject();
|
||||
FontHashConstIt it = hash->constFind(metaObj->className());
|
||||
const FontHashConstIt cend = hash->constEnd();
|
||||
while (it == cend && metaObj != &QWidget::staticMetaObject) {
|
||||
metaObj = metaObj->superClass();
|
||||
it = hash->constFind(metaObj->className());
|
||||
}
|
||||
if (it != cend)
|
||||
return it.value();
|
||||
for (it = hash->constBegin(); it != cend; ++it) {
|
||||
if (widget->inherits(it.key()))
|
||||
return it.value();
|
||||
}
|
||||
}
|
||||
return font();
|
||||
}
|
||||
|
@ -54,6 +54,7 @@
|
||||
#include <QtWidgets/QMainWindow>
|
||||
#include <QtWidgets/QScrollArea>
|
||||
#include <QtWidgets/QScrollBar>
|
||||
#include <QtWidgets/QHeaderView>
|
||||
#include <QtWidgets/private/qapplication_p.h>
|
||||
#include <QtWidgets/QStyle>
|
||||
#include <QtWidgets/qproxystyle.h>
|
||||
@ -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<int>("argc_in");
|
||||
|
Loading…
x
Reference in New Issue
Block a user