Results 1 to 4 of 4

Thread: QDataStream data into QByteArray

  1. #1
    Join Date
    Jun 2014
    Posts
    13
    Thanks
    1
    Qt products
    Qt5

    Default QDataStream data into QByteArray

    I am saving the contents of several variables using QDataStream.

    Qt Code:
    1. out << objWidth << "\n" << objHeight << "\n";
    To copy to clipboard, switch view to plain text mode 

    Where objWidth and objHeight are a single byte (unsigned char, value from 0 to 255).

    then it outputs another series of bytes from a vector

    Qt Code:
    1. for(int i = 0; i < objWidth; i++)
    2. {
    3. for(int j = 0; j < objHeight; j++)
    4. {
    5. byte value = getCellValue(i, j);
    6. out << value << ",";
    7. }
    8. }
    To copy to clipboard, switch view to plain text mode 


    Then it does it again after separating data with a new line character with another vector of data, the exact same dimensions, only different data, but same statement.

    so the whole file would look like:

    Qt Code:
    1. objWidth
    2. \n
    3. objHeight
    4. \n
    5. Vector1 data each element separated with a comma
    6. \n
    7. Vector2 data each element separated with a comma
    To copy to clipboard, switch view to plain text mode 

    Now when I read this data back from a file using QDataStream, do I want to process it directly from a file, or read it into QByteArray so I can process it there, removing new lines and commas etc?

    Which is easier?

  2. #2
    Join Date
    Oct 2009
    Posts
    483
    Thanked 97 Times in 94 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QDataStream data into QByteArray

    You seem to expect a text file format. Shouldn't you be using QTextStream instead?

    Regardless of whether you use a QDataStream or a QTextStream, you need not load everything in a QByteArray. You can read from any QIODevice, including a QFile. Just remember to call status() to detect failures.

  3. #3
    Join Date
    Jun 2014
    Posts
    13
    Thanks
    1
    Qt products
    Qt5

    Default Re: QDataStream data into QByteArray

    Quote Originally Posted by yeye_olive View Post
    You seem to expect a text file format. Shouldn't you be using QTextStream instead?

    Regardless of whether you use a QDataStream or a QTextStream, you need not load everything in a QByteArray. You can read from any QIODevice, including a QFile. Just remember to call status() to detect failures.
    I would ordinarily go with a textformat for this, since it seems easily enough, but further on later, I'm going to be saving an image directly with the file, so I should use QDataStream for this file, correct? OR is there another easier way to do it?
    Last edited by bnosam; 21st July 2014 at 18:50.

  4. #4
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: QDataStream data into QByteArray

    If this data is being written for the same (or another) Qt program to read then you might be better off using QDataStream to serialise the QVector and QImage/QPixmap etc. As it is you are not really using much of the capability of QDataStream so you could possibly just use the basic QIODevice functions to read() and write(). Use QFile or QBuffer for from-disk or in-memory data respectively.

    As it is now you will likely have a jumble of printable and non-printable chars in the file. If you want an on-disk format you can read in a text editor you are better off with QTextStream. When you write the image base 64 encode it so you get printable characters only.

Similar Threads

  1. Help with QDataStream and QByteArray
    By P@u1 in forum Qt Programming
    Replies: 1
    Last Post: 28th June 2011, 00:25
  2. QByteArray,QDataStream Question
    By aash_89 in forum Qt Programming
    Replies: 3
    Last Post: 21st July 2010, 21:40
  3. Replies: 9
    Last Post: 25th July 2009, 13:27
  4. how QDataStream and QByteArray related
    By dognzhe in forum Qt Programming
    Replies: 2
    Last Post: 7th May 2009, 08:45
  5. Reading QByteArray from QDataStream Qt3.3
    By high_flyer in forum Qt Programming
    Replies: 2
    Last Post: 1st April 2006, 20:23

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.