Deprecate QGuiApplication::fontChanged() signal

Rather than have a fontChanged() signal which can be connected to for
tracking when the application font has changed, then it is better to
use the event that is sent to all windows and the application itself.
That way it is easy for a window/widget or item that cares about the
change to the application font to catch it in the event() function.

[ChangeLog][QtGui][QGuiApplication] Deprecated fontChanged() signal in
favor of QEvent::ApplicationFontChanged.

Change-Id: Iae8e832238fc85e385a52305bc04f16e597454b0
Reviewed-by: Volker Hilsheimer <volker.hilsheimer@qt.io>
This commit is contained in:
Andy Shaw 2020-04-22 13:56:38 +02:00
parent 44e8c90ad4
commit dddd197d42
4 changed files with 58 additions and 20 deletions

View File

@ -1900,6 +1900,11 @@ bool QGuiApplication::event(QEvent *e)
if (topLevelWindow->flags() != Qt::Desktop)
postEvent(topLevelWindow, new QEvent(QEvent::LanguageChange));
}
} else if (e->type() == QEvent::ApplicationFontChange) {
for (auto *topLevelWindow : QGuiApplication::topLevelWindows()) {
if (topLevelWindow->flags() != Qt::Desktop)
postEvent(topLevelWindow, new QEvent(QEvent::ApplicationFontChange));
}
} else if (e->type() == QEvent::Quit) {
// Close open windows. This is done in order to deliver de-expose
// events while the event loop is still running.
@ -3408,8 +3413,10 @@ void QGuiApplicationPrivate::applyWindowGeometrySpecificationTo(QWindow *window)
/*!
\since 5.11
\fn void QGuiApplication::fontChanged(const QFont &font)
\obsolete
This signal is emitted when the \a font of the application changes.
This signal is emitted when the \a font of the application changes. Use
QEvent::ApplicationFontChanged instead.
\sa font()
*/
@ -3450,6 +3457,8 @@ void QGuiApplication::setFont(const QFont &font)
auto font = *QGuiApplicationPrivate::app_font;
locker.unlock();
emit qGuiApp->fontChanged(font);
QEvent event(QEvent::ApplicationFontChange);
QGuiApplication::sendEvent(qGuiApp, &event);
}
}

View File

@ -190,8 +190,9 @@ Q_SIGNALS:
#endif
void paletteChanged(const QPalette &pal);
void applicationDisplayNameChanged();
#if QT_DEPRECATED_SINCE(6, 0)
void fontChanged(const QFont &font);
#endif
protected:
bool event(QEvent *) override;
bool compressEvent(QEvent *, QObject *receiver, QPostEventList *) override;

View File

@ -1356,26 +1356,20 @@ QFont QApplication::font(const char *className)
void QApplication::setFont(const QFont &font, const char *className)
{
bool all = false;
FontHash *hash = app_fonts();
if (!className) {
QGuiApplication::setFont(font);
if (hash && hash->size()) {
all = true;
if (hash && hash->size())
hash->clear();
}
} else if (hash) {
hash->insert(className, font);
}
if (QApplicationPrivate::is_app_running && !QApplicationPrivate::is_app_closing) {
// Send ApplicationFontChange to qApp itself, and to the widgets.
QEvent e(QEvent::ApplicationFontChange);
QCoreApplication::sendEvent(QApplication::instance(), &e);
QWidgetList wids = QApplication::allWidgets();
for (QWidgetList::ConstIterator it = wids.constBegin(), cend = wids.constEnd(); it != cend; ++it) {
QWidget *w = *it;
if (all || (!className && w->isWindow()) || w->inherits(className)) // matching class
if (!w->isWindow() && w->inherits(className)) // matching class
sendEvent(w, &e);
}
@ -1740,14 +1734,14 @@ bool QApplication::event(QEvent *e)
#endif
}
if(e->type() == QEvent::LanguageChange) {
if (e->type() == QEvent::LanguageChange || e->type() == QEvent::ApplicationFontChange) {
// QGuiApplication::event does not account for the cases where
// there is a top level widget without a window handle. So they
// need to have the event posted here
const QWidgetList list = topLevelWidgets();
for (auto *w : list) {
if (!w->windowHandle() && (w->windowType() != Qt::Desktop))
postEvent(w, new QEvent(QEvent::LanguageChange));
postEvent(w, new QEvent(e->type()));
}
}

View File

@ -417,6 +417,7 @@ private slots:
void winIdAfterClose();
void receivesLanguageChangeEvent();
void receivesApplicationFontChangeEvent();
private:
bool ensureScreenSize(int width, int height);
@ -11626,30 +11627,36 @@ void tst_QWidget::winIdAfterClose()
delete spy;
}
class LanguageChangeEventWidget : public QWidget
class ChangeEventWidget : public QWidget
{
public:
LanguageChangeEventWidget(QWidget *parent = nullptr) : QWidget(parent) {}
ChangeEventWidget(QWidget *parent = nullptr) : QWidget(parent) {}
int languageChangeCount = 0;
int applicationFontChangeCount = 0;
protected:
bool event(QEvent *e) override
{
if (e->type() == QEvent::LanguageChange)
languageChangeCount++;
else if (e->type() == QEvent::ApplicationFontChange)
applicationFontChangeCount++;
return QWidget::event(e);
}
};
class LanguageChangeEventWindow : public QWindow
class ChangeEventWindow : public QWindow
{
public:
LanguageChangeEventWindow(QWindow *parent = nullptr) : QWindow(parent) {}
ChangeEventWindow(QWindow *parent = nullptr) : QWindow(parent) {}
int languageChangeCount = 0;
int applicationFontChangeCount = 0;
protected:
bool event(QEvent *e) override
{
if (e->type() == QEvent::LanguageChange)
languageChangeCount++;
else if (e->type() == QEvent::ApplicationFontChange)
applicationFontChangeCount++;
return QWindow::event(e);
}
};
@ -11658,14 +11665,14 @@ void tst_QWidget::receivesLanguageChangeEvent()
{
// Confirm that any QWindow or QWidget only gets a single
// LanguageChange event when a translator is installed
LanguageChangeEventWidget topLevel;
auto childWidget = new LanguageChangeEventWidget(&topLevel);
ChangeEventWidget topLevel;
auto childWidget = new ChangeEventWidget(&topLevel);
topLevel.show();
QVERIFY(QTest::qWaitForWindowExposed(&topLevel));
LanguageChangeEventWindow ww;
ChangeEventWindow ww;
ww.show();
QVERIFY(QTest::qWaitForWindowExposed(&ww));
LanguageChangeEventWidget topLevelNotShown;
ChangeEventWidget topLevelNotShown;
QTranslator t;
QVERIFY(t.load("hellotr_la.qm", ":/"));
QVERIFY(qApp->installTranslator(&t));
@ -11676,5 +11683,32 @@ void tst_QWidget::receivesLanguageChangeEvent()
QCOMPARE(ww.languageChangeCount, 1);
}
void tst_QWidget::receivesApplicationFontChangeEvent()
{
// Confirm that any QWindow or top level QWidget only gets a single
// ApplicationFontChange event when the font is changed
const QFont origFont = QApplication::font();
ChangeEventWidget topLevel;
auto childWidget = new ChangeEventWidget(&topLevel);
topLevel.show();
QVERIFY(QTest::qWaitForWindowExposed(&topLevel));
ChangeEventWindow ww;
ww.show();
QVERIFY(QTest::qWaitForWindowExposed(&ww));
ChangeEventWidget topLevelNotShown;
QFont changedFont = origFont;
changedFont.setPointSize(changedFont.pointSize() + 2);
QApplication::setFont(changedFont);
QCoreApplication::sendPostedEvents(0, QEvent::ApplicationFontChange);
QCOMPARE(topLevel.applicationFontChangeCount, 1);
QCOMPARE(topLevelNotShown.applicationFontChangeCount, 1);
// QWidget should not be passing the event on automatically
QCOMPARE(childWidget->applicationFontChangeCount, 0);
QCOMPARE(ww.applicationFontChangeCount, 1);
QApplication::setFont(origFont);
}
QTEST_MAIN(tst_QWidget)
#include "tst_qwidget.moc"