summaryrefslogtreecommitdiffstats
path: root/tests
diff options
context:
space:
mode:
authorTor Arne Vestbø <tor.arne.vestbo@qt.io>2020-01-08 17:48:58 +0100
committerTor Arne Vestbø <tor.arne.vestbo@qt.io>2020-01-13 16:53:05 +0100
commit0a93db4d82c051164923a10e4382b12de9049b45 (patch)
tree0a5a1a03fb15ac4b73a6197a565688da67c6cd95 /tests
parent7e2cef0f15b133c07bd11aa5e5065b0b9959fb0d (diff)
Unify application palette handling between QGuiApplication and QApplication
The logic is now mostly handled in QGuiApplication, with QApplication only dealing with the widget-specific palettes and interaction between the style and the palette. The application now picks up changes to the platform theme and will re-resolve the current application palette appropriately. This also works even if an explicit application palette has been set, in which case any missing roles are filled in by the theme. The palette can now also be reset back to the default application palette that's fully based on the theme, by passing in the default constructed palette (or any palette that doesn't have any roles set). This is also correctly reflected in the Qt::AA_SetPalette attribute. Conceptually this means QGuiApplication and QApplication follow the same behavior as QWidget, where the palette falls back to a base or inherited palette for roles that are not set, in this case the theme. Behavior-wise this means that the default application palette of the application does not have any roles set, but clients should not have relied on this, nor does QWidget rely on that internally. It also means that setting a palette on the application and then getting it back again will not produce the same palette as set, since the palette was resolved against the theme in the meantime. This is the same behavior as for QWidget, and although it's a behavior change it's one towards a more sane behavior, so we accept it. [ChangeLog] Application palettes are now resolved against the platform's theme palette, the same way widget palettes are resolved against their parents, and the application palette. This means the application palette reflected through QGuiApplication::palette() may not be exactly the same palette as set via QGuiApplication::setPalette(). Change-Id: I76b99fcd27285e564899548349aa2a5713e5965d Reviewed-by: Vitaly Fanaskov <vitaly.fanaskov@qt.io> Reviewed-by: Friedemann Kleint <Friedemann.Kleint@qt.io>
Diffstat (limited to 'tests')
-rw-r--r--tests/auto/gui/kernel/qguiapplication/tst_qguiapplication.cpp51
1 files changed, 46 insertions, 5 deletions
diff --git a/tests/auto/gui/kernel/qguiapplication/tst_qguiapplication.cpp b/tests/auto/gui/kernel/qguiapplication/tst_qguiapplication.cpp
index 5ee25677bf..a7881b6983 100644
--- a/tests/auto/gui/kernel/qguiapplication/tst_qguiapplication.cpp
+++ b/tests/auto/gui/kernel/qguiapplication/tst_qguiapplication.cpp
@@ -506,29 +506,67 @@ void tst_QGuiApplication::keyboardModifiers()
window->close();
}
+/*
+ Compare actual against expected but ignore unset roles.
+
+ Comparing palettes via operator== will compare all roles.
+*/
+static bool palettesMatch(const QPalette &actual, const QPalette &expected)
+{
+ if (actual.resolve() != expected.resolve())
+ return false;
+
+ for (int i = 0; i < QPalette::NColorGroups; i++) {
+ for (int j = 0; j < QPalette::NColorRoles; j++) {
+ const auto g = QPalette::ColorGroup(i);
+ const auto r = QPalette::ColorRole(j);
+ if (expected.isBrushSet(g, r)) {
+ if (actual.brush(g, r) != expected.brush(g, r))
+ return false;
+ }
+ }
+ }
+ return true;
+}
+
void tst_QGuiApplication::palette()
{
+ // Getting the palette before application construction should work
+ QPalette paletteBeforeAppConstruction = QGuiApplication::palette();
+ // And should be reflected in the default constructed palette
+ QCOMPARE(paletteBeforeAppConstruction, QPalette());
+
int argc = 1;
char *argv[] = { const_cast<char*>("tst_qguiapplication") };
QGuiApplication app(argc, argv);
+
+ // The same should be true after application construction
+ QCOMPARE(QGuiApplication::palette(), QPalette());
+
+ // The default application palette is not resolved
+ QVERIFY(!QGuiApplication::palette().resolve());
+
QSignalSpy signalSpy(&app, SIGNAL(paletteChanged(QPalette)));
QPalette oldPalette = QGuiApplication::palette();
QPalette newPalette = QPalette(Qt::red);
QGuiApplication::setPalette(newPalette);
- QCOMPARE(QGuiApplication::palette(), newPalette);
+ QVERIFY(palettesMatch(QGuiApplication::palette(), newPalette));
QCOMPARE(signalSpy.count(), 1);
- QCOMPARE(signalSpy.at(0).at(0), QVariant(newPalette));
+ QVERIFY(palettesMatch(signalSpy.at(0).at(0).value<QPalette>(), newPalette));
+ QCOMPARE(QGuiApplication::palette(), QPalette());
QGuiApplication::setPalette(oldPalette);
- QCOMPARE(QGuiApplication::palette(), oldPalette);
+ QVERIFY(palettesMatch(QGuiApplication::palette(), oldPalette));
QCOMPARE(signalSpy.count(), 2);
- QCOMPARE(signalSpy.at(1).at(0), QVariant(oldPalette));
+ QVERIFY(palettesMatch(signalSpy.at(1).at(0).value<QPalette>(), oldPalette));
+ QCOMPARE(QGuiApplication::palette(), QPalette());
QGuiApplication::setPalette(oldPalette);
- QCOMPARE(QGuiApplication::palette(), oldPalette);
+ QVERIFY(palettesMatch(QGuiApplication::palette(), oldPalette));
QCOMPARE(signalSpy.count(), 2);
+ QCOMPARE(QGuiApplication::palette(), QPalette());
}
void tst_QGuiApplication::font()
@@ -1073,6 +1111,9 @@ void tst_QGuiApplication::testSetPaletteAttribute()
QGuiApplication::setPalette(palette);
QVERIFY(QCoreApplication::testAttribute(Qt::AA_SetPalette));
+
+ QGuiApplication::setPalette(QPalette());
+ QVERIFY(!QCoreApplication::testAttribute(Qt::AA_SetPalette));
}
// Test that static functions do not crash if there is no application instance.