Results 1 to 2 of 2

Thread: Serialization with QDataStream

  1. #1
    Join Date
    Jun 2018
    Posts
    29
    Thanks
    7
    Qt products
    Qt5
    Platforms
    MacOS X Windows

    Default Serialization with QDataStream

    Hi,
    How can I do the serialization of a QString?
    I have executed the example code that is in the QDataStream class, but it does not do the serialization of the QString, only of the integer, the output in the file is the following: the answer *
    And the code is as follows:

    QFile file("/Users/eduardo/My Cloud/Programas/readFileLineToLine12/dat1.txt");
    if (!file.open(QIODevice::WriteOnly/* | QIODevice::Append*/)) {
    QMessageBox::information(this, tr("Unable to open file"), file.errorString());
    return;
    }
    QDataStream out(&file);
    out.setVersion(QDataStream::Qt_5_4);
    out << QString("the answer is");
    out << (qint32)42;
    file.flush();
    file.close();

    Thanks a lot

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,581
    Thanks
    13
    Thanked 1,584 Times in 1,513 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Serialization with QDataStream

    Quote Originally Posted by Eduardo Huerta View Post
    How can I do the serialization of a QString?
    Like you have done. QDataStream serialiszs in a binary format. Your code outputs this binary data into the file (offsets and data in hex):
    Qt Code:
    1. $ od -A x -tx1 dat1.bin
    2. 000000 00 00 00 1a 00 74 00 68 00 65 00 20 00 61 00 6e
    3. 000010 00 73 00 77 00 65 00 72 00 20 00 69 00 73 00 00
    4. 000020 00 2a
    To copy to clipboard, switch view to plain text mode 
    The QString is the length in bytes (00 00 00 1a i.e. 26 ) followed by the bytes that make up the 13 chars of text "the answer is".
    The integer 42 is the four bytes 00 00 00 2a
    Total file length is 34 bytes.

    If you treat the binary file as text (like you have) then the first five bytes are non-printable, followed by "t", another non-printable, "h" ..., and the last byte is 0x42 corresponding to an asterisk.


    If you want to see the text followed by 42, like "the answer is 42", then you should look at QTextStream.

Similar Threads

  1. Qlist Serialization by qdatastream error
    By WezSieTato in forum Newbie
    Replies: 2
    Last Post: 13th August 2014, 16:38
  2. Object serialization with QDataStream
    By jahsiotr in forum Newbie
    Replies: 1
    Last Post: 20th January 2013, 23:04
  3. QDataStream and serialization
    By pdoria in forum Qt Programming
    Replies: 5
    Last Post: 11th November 2009, 10:42
  4. QDataStream-serialization not writting to file
    By john_god in forum Qt Programming
    Replies: 4
    Last Post: 1st August 2009, 13:27
  5. [Qt4] qdatastream/qstring serialization
    By fabo in forum Qt Programming
    Replies: 4
    Last Post: 19th April 2006, 19:31

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.