QDataStream: Fix inconsistent results of iostream with QPalette objects

The value of NColorRoles got changed since 5.11. It introduced one more
role called "PlaceholderText" in the ColorRole enumeration.

When using QDataStream (5.12) to read QPalette objects from a file
written by 5.9 (<5.11), the processing results are inconsistent.

Fixes: QTBUG-74885
Change-Id: I14d57f9603a26e5890b4fd57c7e464c5b38eb3f2
Reviewed-by: Eirik Aavitsland <eirik.aavitsland@qt.io>
This commit is contained in:
Ryan Chu 2019-05-03 16:14:19 +02:00
parent 14aa1f7d6f
commit cc4c0b43a5
2 changed files with 48 additions and 11 deletions

View File

@ -1006,6 +1006,8 @@ QDataStream &operator<<(QDataStream &s, const QPalette &p)
max = QPalette::HighlightedText + 1; max = QPalette::HighlightedText + 1;
else if (s.version() <= QDataStream::Qt_4_3) else if (s.version() <= QDataStream::Qt_4_3)
max = QPalette::AlternateBase + 1; max = QPalette::AlternateBase + 1;
else if (s.version() <= QDataStream::Qt_5_11)
max = QPalette::ToolTipText + 1;
for (int r = 0; r < max; r++) for (int r = 0; r < max; r++)
s << p.d->br[grp][r]; s << p.d->br[grp][r];
} }
@ -1046,6 +1048,9 @@ QDataStream &operator>>(QDataStream &s, QPalette &p)
} else if (s.version() <= QDataStream::Qt_4_3) { } else if (s.version() <= QDataStream::Qt_4_3) {
p = QPalette(); p = QPalette();
max = QPalette::AlternateBase + 1; max = QPalette::AlternateBase + 1;
} else if (s.version() <= QDataStream::Qt_5_11) {
p = QPalette();
max = QPalette::ToolTipText + 1;
} }
QBrush tmp; QBrush tmp;

View File

@ -174,6 +174,7 @@ private slots:
void floatingPointPrecision(); void floatingPointPrecision();
void compatibility_Qt5();
void compatibility_Qt3(); void compatibility_Qt3();
void compatibility_Qt2(); void compatibility_Qt2();
@ -260,17 +261,17 @@ static int NColorRoles[] = {
QPalette::HighlightedText + 1, // Qt_4_0, Qt_4_1 QPalette::HighlightedText + 1, // Qt_4_0, Qt_4_1
QPalette::HighlightedText + 1, // Qt_4_2 QPalette::HighlightedText + 1, // Qt_4_2
QPalette::AlternateBase + 1, // Qt_4_3 QPalette::AlternateBase + 1, // Qt_4_3
QPalette::PlaceholderText + 1, // Qt_4_4 QPalette::ToolTipText + 1, // Qt_4_4
QPalette::PlaceholderText + 1, // Qt_4_5 QPalette::ToolTipText + 1, // Qt_4_5
QPalette::PlaceholderText + 1, // Qt_4_6 QPalette::ToolTipText + 1, // Qt_4_6, Qt_4_7, Qt_4_8, Qt_4_9
QPalette::PlaceholderText + 1, // Qt_5_0 QPalette::ToolTipText + 1, // Qt_5_0
QPalette::PlaceholderText + 1, // Qt_5_1 QPalette::ToolTipText + 1, // Qt_5_1
QPalette::PlaceholderText + 1, // Qt_5_2 QPalette::ToolTipText + 1, // Qt_5_2, Qt_5_3
QPalette::PlaceholderText + 1, // Qt_5_3 QPalette::ToolTipText + 1, // Qt_5_4, Qt_5_5
QPalette::PlaceholderText + 1, // Qt_5_4 QPalette::ToolTipText + 1, // Qt_5_6, Qt_5_7, Qt_5_8, Qt_5_9, Qt_5_10, Qt_5_11
QPalette::PlaceholderText + 1, // Qt_5_5 QPalette::PlaceholderText + 1, // Qt_5_12
QPalette::PlaceholderText + 1, // Qt_5_6 QPalette::PlaceholderText + 1, // Qt_5_13
0 // add the correct value for Qt_5_7 here later 0 // add the correct value for Qt_5_14 here later
}; };
// Testing get/set functions // Testing get/set functions
@ -3102,6 +3103,37 @@ void tst_QDataStream::streamRealDataTypes()
} }
} }
void tst_QDataStream::compatibility_Qt5()
{
QLinearGradient gradient(QPointF(0,0), QPointF(1,1));
gradient.setColorAt(0, Qt::red);
gradient.setColorAt(1, Qt::blue);
QBrush brush(gradient);
QPalette palette;
palette.setBrush(QPalette::Button, brush);
palette.setColor(QPalette::Light, Qt::green);
QByteArray stream;
{
QDataStream out(&stream, QIODevice::WriteOnly);
out.setVersion(QDataStream::Qt_5_7);
out << palette;
out << brush;
}
QBrush in_brush;
QPalette in_palette;
{
QDataStream in(stream);
in.setVersion(QDataStream::Qt_5_7);
in >> in_palette;
in >> in_brush;
}
QCOMPARE(in_brush.style(), Qt::LinearGradientPattern);
QCOMPARE(in_palette.brush(QPalette::Button).style(), Qt::LinearGradientPattern);
QCOMPARE(in_palette.color(QPalette::Light), QColor(Qt::green));
}
void tst_QDataStream::compatibility_Qt3() void tst_QDataStream::compatibility_Qt3()
{ {
QByteArray ba("hello"); QByteArray ba("hello");