Junk data stream....
-
Using qt 6.8 instead of 5.15, QDataStream has become some junk full of crap... Doing even a basic save of int, causes some junk to be added at the beginning of the stream, if i output an int to the file, the int has 4 bytes, but in the file it has 8....
And to read the value i have to shift the data by 4 bytes, the problem is it doesn't work with a bigger set of data for unknown reasons even simple operations result in some random junk being created, the data is getting corrupted and i have no plans to waste any more time especially that it's related to some alien implementation in qt 6.8...
If i open the file in qt, in binary viewer, a simple saved int, then i will get "00 00 00 00 00 00 00 02" as the data, WHAT is this junk of 0's added? It should be "00 00 00 02" NOT "00 00 00 00 00 00 00 02"....I need some info what's going on, i downloaded that qt 6.8 recently and i regret it already....
-
First, try to lower the level of aggression a notch, no one here did anything bad to you I hope.
Anyway, two things:
-
The byte pattern is consistent with that of a big-endian 64 bit integer. I assume that you already double checked that in the Qt 6 build it still holds that
sizeof(int) == 4
. So you might have been bitten by type promotion rules - note that in theQDataStream
documentation one can clearly see that all integral overloads ofoperator<<
take fixed sizes type and not the platform dependent types). You can try to serialize aint32_t
(or the Qt specific typedefqint32
) to see if you get the right result. And in general, always work with the fixed typedefs for binary serialization. -
The protocol of
QDataStream
changes between Qt versions. If you need compatibility to the protocol as it was in 5.15, you need to ask for it.
-
-
To quote the fine manual: "For integers it is best to always cast to a Qt integer type for writing, and to read back into the same Qt integer type. This ensures that you get integers of the size you want and insulates you from compiler and platform differences."
Since you've provided neither code nor platform information it is hard to be specific about the way your code is behaving. Here's an example:
#include <QCoreApplication> #include <QDataStream> #include <QBuffer> #include <QDebug> int main(int argc, char *argv[]) { QCoreApplication a(argc, argv); int test = 2; qDebug() << "sizeof(test) ==" << sizeof(test); QBuffer out; if (out.open(QIODevice::WriteOnly)) { QDataStream stream(&out); stream.setVersion(QDataStream::Qt_6_8); // default versioning stream << static_cast<qint32>(test); out.close(); qDebug() << "streamed output ==" << out.size() << "bytes:" << out.buffer().toHex(' '); } if (out.open(QIODevice::WriteOnly)) { QDataStream stream(&out); stream.setVersion(QDataStream::Qt_5_15); stream << static_cast<qint32>(test); out.close(); qDebug() << "streamed output ==" << out.size() << "bytes:" << out.buffer().toHex(' '); } if (out.open(QIODevice::ReadOnly)) { QDataStream stream(&out); stream.setVersion(QDataStream::Qt_5_15); int test1; stream >> test1; out.close(); qDebug() << "Read back into int ==" << test1; } // Read the Qt 5.15 stream from Qt 6.8 if (out.open(QIODevice::ReadOnly)) { QDataStream stream(&out); stream.setVersion(QDataStream::Qt_6_8); int test1; stream >> test1; out.close(); qDebug() << "Read back into int ==" << test1; } return 0; }
That provides this result on Windows 11, MingW 13, Qt 6.8.1:
sizeof(test) == 4 streamed output == 4 bytes: "00 00 00 02" streamed output == 4 bytes: "00 00 00 02" Read back into int == 2 Read back into int == 2