Skip to content

Commit c95566f

Browse files
committed
GUI: forward styling/theming relevant events to the QStyleHints object
QStyleHints is a singleton that provides both Widgets and Quick with information about system properties, including the color scheme. The system palette and font however are provided by QGuiApplication, which informs all windows via events when those change. The styling infrastructure in Qt Quick Controls knows about QStyleHints, but QGuiApplication only knows about the list of QWindow instances. There is no specific "style object" that QGuiApplication knows about and could inform about theme changes. On the Widgets side, QApplication knows about the QStyle instance, and uses polishing to let the style adjust the palette. This makes it cumbersome for Qt Quick Control styles to react to changes to global theme attributes like the palette or the font, as we would either have to filter events on QGuiApplication - very costly as all events to all objects pass through such an event filter - or maintain a list of windows to filter events on. QStyleHints is the most appropriate object we have for a UI-framework independent global "style object" that can get informed about theme changes using the established event machinery. It makes sense to forward theming-related events to this object, which then allows Qt Quick Controls to filter events on that instance, avoiding the cost of an application event filter. Since the QApplication test case counts exact number of events seen by a global event filter, adjust it by first flushing the events that now get posted to QStyleHints when the palette gets initialized, and then including the additional event seen by the filter. It also provides us with an opportunity to clean up the entanglement of QPlatformTheme implementations updating the QStyleHints object directly when accessibility settings or color scheme changes: this could now be done in a QStyleHints::event implementation, assuming that such changes are going through event delivery. This is left for a future commit. For now, we simply send relevant events to the QStyleHints object to enable fixes and improvements in Qt Quick Controls' system theme awareness. Pick-to: 6.8 Task-number: QTBUG-140507 Change-Id: Ib00bdb2f31fa124b572b68dd195253baafff3d3d Reviewed-by: Tor Arne Vestbø <tor.arne.vestbo@qt.io> (cherry picked from commit 8e7b97b) Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org> (cherry picked from commit ed0e2f9) Reviewed-by: Volker Hilsheimer <volker.hilsheimer@qt.io>
1 parent b9f112a commit c95566f

File tree

2 files changed

+8
-3
lines changed

2 files changed

+8
-3
lines changed

src/gui/kernel/qguiapplication.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2076,12 +2076,14 @@ bool QGuiApplication::event(QEvent *e)
20762076
break;
20772077
case QEvent::ApplicationFontChange:
20782078
case QEvent::ApplicationPaletteChange:
2079+
postEvent(QGuiApplication::styleHints(), e->clone());
20792080
for (auto *topLevelWindow : QGuiApplication::topLevelWindows()) {
20802081
if (topLevelWindow->flags() != Qt::Desktop)
20812082
postEvent(topLevelWindow, new QEvent(e->type()));
20822083
}
20832084
break;
20842085
case QEvent::ThemeChange:
2086+
forwardEvent(QGuiApplication::styleHints(), e);
20852087
for (auto *w : QGuiApplication::allWindows())
20862088
forwardEvent(w, e);
20872089
break;

tests/auto/widgets/kernel/qapplication/tst_qapplication.cpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,7 @@ QT_WARNING_POP
263263
}
264264
};
265265

266+
QApplication::sendPostedEvents();
266267
EventWatcher watcher;
267268

268269
QCOMPARE(watcher.palette_changed, 0);
@@ -273,11 +274,13 @@ QT_WARNING_POP
273274
qApp->setFont(font);
274275
QApplication::processEvents();
275276
#if QT_DEPRECATED_SINCE(6, 0)
277+
// signal, plus events delivered to qApp and QStyleHints
278+
QCOMPARE(watcher.palette_changed, 3);
279+
QCOMPARE(watcher.font_changed, 3);
280+
#else
281+
// events delivered to qApp and QStyleHints
276282
QCOMPARE(watcher.palette_changed, 2);
277283
QCOMPARE(watcher.font_changed, 2);
278-
#else
279-
QCOMPARE(watcher.palette_changed, 1);
280-
QCOMPARE(watcher.font_changed, 1);
281284
#endif
282285
}
283286

0 commit comments

Comments
 (0)