Results 1 to 3 of 3

Thread: Reading QByteArray from QDataStream Qt3.3

  1. #1
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Reading QByteArray from QDataStream Qt3.3

    Hi,

    I am overloading the operator>>() and operator<<() for QDataStream to serialize my own data blocks.
    For writing in to binary file:
    Qt Code:
    1. QDataStream & operator<<( QDataStream &s, AtmosBBlock &data)
    2. {
    3. QByteArray b_data;
    4. b_data.resize(sizeof(data));
    5. memcpy(b_data.begin(),&data,sizeof(data));
    6. s<<b_data;
    7. return s;
    8. }
    To copy to clipboard, switch view to plain text mode 
    For reading from binary file:
    Qt Code:
    1. QDataStream & operator>>( QDataStream &s, AtmosBBlock &data)
    2. {
    3. QByteArray b_data;
    4. b_data.resize(sizeof(data));
    5. s>>b_data;
    6. memcpy(&data,b_data.begin(),sizeof(data));
    7. return s;
    8. }
    To copy to clipboard, switch view to plain text mode 
    The problem is, that s>>b_data is resulting in a 0 sized QByteArray, and I don't understand why.
    To check if the problem is in the written file I did the following:
    Qt Code:
    1. QDataStream & operator>>( QDataStream &s, AtmosBBlock &data)
    2. {
    3. QByteArray b_data;
    4. b_data.resize(sizeof(data));
    5. s.readRawBytes((char*)&b_data,sizeof(b_data));
    6. memcpy(&data,b_data.begin(),sizeof(data));
    7. return s;
    8. }
    To copy to clipboard, switch view to plain text mode 
    This reads the file correctly with the previously written data.

    Any idea why s>>b_data returns a 0 sized QByteArray?

    Thanks in adavance.

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Reading QByteArray from QDataStream Qt3.3

    I think >> expects not only the data itself but also a field which tells how much data to read. ReadRawBytes on the other hand takes the size as its argument. Those two calls are not equivalent. I understand that if you write something with << you should be able to read it with >>, but looks like it fails here. It could be that sizeof() returns invalid size here. Why do you use memcpy here? What is AtmosBBlock?

  3. #3
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Reading QByteArray from QDataStream Qt3.3

    I think >> expects not only the data itself but also a field which tells how much data to read.
    How do you mean?
    The API does not include any extra parameter just a QBayteArray:
    QDataStream & operator>> ( QDataStream & s, QByteArray & a )

    Reads a byte array into a from the stream s and returns a reference to the stream.
    I thought it could be that QDataStream uses QBatesArray::size() to detrmine the amount of bytes to read, which is why I resize it before the read.
    But it doesn't work.
    I am well aware that readRawBytes and >> are different, but I still can't understand why >> with a QByteArray does not work, when according to the docs it should (unless I am missing here somthing in the docs).

    AtmosBBlock is a struct with natural types (Q_UINT8 and Q_UINT16)

    Thanks.

Similar Threads

  1. QDataStream and QByteArray issue
    By sepehr in forum Qt Programming
    Replies: 13
    Last Post: 2nd April 2021, 17:41
  2. Reading from QByteArray
    By Misenko in forum Qt Programming
    Replies: 1
    Last Post: 30th September 2008, 17:17
  3. QWT 5, QT3, SuSE 10.2. Crash and burn
    By DrMcCleod in forum Qwt
    Replies: 8
    Last Post: 7th September 2007, 21:53
  4. Replies: 8
    Last Post: 27th August 2007, 16:45
  5. QDataStream reading into QString
    By jakamph in forum Qt Programming
    Replies: 4
    Last Post: 15th October 2006, 10:22

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.