From aa45401ffbb5bfad5d43aada664946eaf062747a Mon Sep 17 00:00:00 2001 From: Volker Hilsheimer Date: Tue, 22 Nov 2022 13:00:22 +0100 Subject: [PATCH] QPalette: always increase serial number when modifying After 109e088c7c5d0c9325966e88d55fd9f7a58f67ea, cache keys were unique for palettes with different private or data instances, but the key did not change when a palette without any shared copies was modified, as that does not create new private data structures. To fix this, always increase the counter for the private data structure, also when not detaching from shared copies. Augment test case with scenario that broke. Fixes: QTBUG-108709 Change-Id: I606abfb8b1a03e515e46b10dc840a631eb31d496 Reviewed-by: Timur Pocheptsov Reviewed-by: JiDe Zhang Reviewed-by: Alessandro Portale (cherry picked from commit ef379f95c7621272933b06fb3edfe502b14bd145) Reviewed-by: Qt Cherry-pick Bot --- src/gui/kernel/qpalette.cpp | 4 +++- tests/auto/gui/kernel/qpalette/tst_qpalette.cpp | 8 ++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/src/gui/kernel/qpalette.cpp b/src/gui/kernel/qpalette.cpp index 081af380745..4abbcd5e655 100644 --- a/src/gui/kernel/qpalette.cpp +++ b/src/gui/kernel/qpalette.cpp @@ -62,7 +62,7 @@ public: QAtomicInt ref; QPalette::ResolveMask resolveMask = {0}; static inline int qt_palette_private_count = 0; - const int detach_no = ++qt_palette_private_count; + int detach_no = ++qt_palette_private_count; QExplicitlySharedDataPointer data; }; @@ -864,6 +864,8 @@ void QPalette::detach() if (!d->ref.deref()) delete d; d = x; + } else { + d->detach_no = ++QPalettePrivate::qt_palette_private_count; } } diff --git a/tests/auto/gui/kernel/qpalette/tst_qpalette.cpp b/tests/auto/gui/kernel/qpalette/tst_qpalette.cpp index 2e9a484b8b2..897511300b2 100644 --- a/tests/auto/gui/kernel/qpalette/tst_qpalette.cpp +++ b/tests/auto/gui/kernel/qpalette/tst_qpalette.cpp @@ -274,6 +274,14 @@ void tst_QPalette::cacheKey() const auto defaultSerNo = defaultCacheKey >> 32; const auto defaultDetachNo = defaultCacheKey & 0xffffffff; + QPalette changeTwicePalette(defaultPalette); + changeTwicePalette.setBrush(QPalette::All, QPalette::ButtonText, Qt::red); + const auto firstChangeCacheKey = changeTwicePalette.cacheKey(); + QCOMPARE_NE(firstChangeCacheKey, defaultCacheKey); + changeTwicePalette.setBrush(QPalette::All, QPalette::ButtonText, Qt::green); + const auto secondChangeCacheKey = changeTwicePalette.cacheKey(); + QCOMPARE_NE(firstChangeCacheKey, secondChangeCacheKey); + QPalette copyDifferentData(defaultPalette); QPalette copyDifferentMask(defaultPalette); QPalette copyDifferentMaskAndData(defaultPalette);