Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Junk data stream....
Forum Updated to NodeBB v4.3 + New Features

Junk data stream....

Scheduled Pinned Locked Moved Unsolved General and Desktop
3 Posts 3 Posters 229 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    Loc888
    wrote on last edited by
    #1

    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....

    1 Reply Last reply
    0
    • I Offline
      I Offline
      IgKh
      wrote on last edited by
      #2

      First, try to lower the level of aggression a notch, no one here did anything bad to you I hope.

      Anyway, two things:

      1. 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 the QDataStream documentation one can clearly see that all integral overloads of operator<< take fixed sizes type and not the platform dependent types). You can try to serialize a int32_t (or the Qt specific typedef qint32) to see if you get the right result. And in general, always work with the fixed typedefs for binary serialization.

      2. 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.

      1 Reply Last reply
      3
      • C Offline
        C Offline
        ChrisW67
        wrote on last edited by
        #3

        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
        
        1 Reply Last reply
        3

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved