Skip to content

Commit

Permalink
QTime: restore Qt3 compatibility in the QDataStream operators
Browse files Browse the repository at this point in the history
A Qt5 program writing a null QTime() using setVersion(QDataStream::Qt_3_3),
and then a Qt3 program reading that, would lead to a weird QTime,
with isNull=false, isValid=false, hour=1193, minute=2, second=47, ms=295.

This commit restores interoperability, by writing out the expected value
(0) for a null QTime rather than the -1 value used by Qt4 and Qt5.

Change-Id: Icde468a8f6fc9434ef7018296725819b44d672af
Reviewed-by: Thiago Macieira <[email protected]>
  • Loading branch information
dfaure-kdab committed Jul 2, 2016
1 parent 9964b85 commit 1af4916
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 3 deletions.
14 changes: 12 additions & 2 deletions src/corelib/tools/qdatetime.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4759,7 +4759,12 @@ QDataStream &operator>>(QDataStream &in, QDate &date)

QDataStream &operator<<(QDataStream &out, const QTime &time)
{
return out << quint32(time.mds);
if (out.version() >= QDataStream::Qt_4_0) {
return out << quint32(time.mds);
} else {
// Qt3 had no support for reading -1, QTime() was valid and serialized as 0
return out << quint32(time.isNull() ? 0 : time.mds);
}
}

/*!
Expand All @@ -4774,7 +4779,12 @@ QDataStream &operator>>(QDataStream &in, QTime &time)
{
quint32 ds;
in >> ds;
time.mds = int(ds);
if (in.version() >= QDataStream::Qt_4_0) {
time.mds = int(ds);
} else {
// Qt3 would write 0 for a null time
time.mds = (ds == 0) ? QTime::NullTime : int(ds);
}
return in;
}

Expand Down
27 changes: 26 additions & 1 deletion tests/auto/corelib/io/qdatastream/tst_qdatastream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1203,10 +1203,11 @@ static QTime qTimeData(int index)
case 57: return QTime(23, 59, 59, 99);
case 58: return QTime(23, 59, 59, 100);
case 59: return QTime(23, 59, 59, 999);
case 60: return QTime();
}
return QTime(0, 0, 0);
}
#define MAX_QTIME_DATA 60
#define MAX_QTIME_DATA 61

void tst_QDataStream::stream_QTime_data()
{
Expand Down Expand Up @@ -3081,6 +3082,30 @@ void tst_QDataStream::compatibility_Qt3()
QVERIFY(in_palette.brush(QPalette::Button).style() == Qt::NoBrush);
QVERIFY(in_palette.color(QPalette::Light) == Qt::green);
}
// QTime() was serialized to (0, 0, 0, 0) in Qt3, not (0xFF, 0xFF, 0xFF, 0xFF)
// This is because in Qt3 a null time was valid, and there was no support for deserializing a value of -1.
{
QByteArray stream;
{
QDataStream out(&stream, QIODevice::WriteOnly);
out.setVersion(QDataStream::Qt_3_3);
out << QTime();
}
QTime in_time;
{
QDataStream in(stream);
in.setVersion(QDataStream::Qt_3_3);
in >> in_time;
}
QVERIFY(in_time.isNull());

quint32 rawValue;
QDataStream in(stream);
in.setVersion(QDataStream::Qt_3_3);
in >> rawValue;
QCOMPARE(rawValue, quint32(0));
}

}

void tst_QDataStream::compatibility_Qt2()
Expand Down

0 comments on commit 1af4916

Please sign in to comment.