[Qt-interest] QDataStream backwards incompatibility introduced in 4.6
Lene Preuss
lene.preuss at googlemail.com
Thu Jan 21 10:50:32 CET 2010
Hi list,
I don't know if this is common knowledge already. It is not mentioned in
http://doc.qt.nokia.com/4.6/qt4-6-intro.html, and a quick google turned
up nothing either.
The QDataStream class changed the way it stores floats from 4.5 to 4.6.
Floats written with Qt prior to 4.6 are not read correctly with 4.6.
Check this out:
#include <iostream>
#include <QFile>
#include <QDataStream>
void writeFile(const QString &filename) {
QFile *file = new QFile(filename);
if (!file->open(QIODevice::WriteOnly)) return;
QDataStream stream(file);
stream.setVersion(11); // QDataStream version used in Qt 4.5
float data[3] = { 1.0, 0.429043, 1.2 };
for (unsigned i = 0; i < 3; ++i)
stream << data[i];
file->close();
}
void readFile(const QString &filename) {
QFile *file = new QFile(filename);
if (!file->open(QIODevice::ReadOnly)) return;
QDataStream stream(file);
stream.setVersion(12); // QDataStream version used in Qt 4.6
for (unsigned i = 0; i < 3; ++i) {
float data;
stream >> data;
std::cout << data << std::endl;
}
}
The output on my system is:
0.0078125
0
0
not, as you might expect,
1.0
0.429043
1.2
It took me a couple of hours to find out why my program was suddenly
unable to load data files, and I suspect this might affect a large
number of applications, so I think you should know.
Greets, Lene
More information about the Qt-interest-old
mailing list