Results 1 to 3 of 3

Thread: how QDataStream and QByteArray related

  1. #1
    Join Date
    May 2009
    Posts
    38
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Windows

    Question how QDataStream and QByteArray related

    I am not sure why every one has to use QDataStream with QByteArray ??
    why???

    please have a look the code
    Qt Code:
    1. QBuffer buffer;
    2. QImageWriter writer(&buffer, "PNG");
    3. writer.write( randomImage() );
    4. [COLOR="red"]QByteArray data;[/COLOR]
    5. QDataStream stream( &data, QIODevice::WriteOnly );
    6. stream << (quint32)buffer.data().size();
    7. data.append( buffer.data() );
    8. socket.write( data );
    To copy to clipboard, switch view to plain text mode 
    this code is to send a picture.
    for same purpose:
    Qt Code:
    1. QBuffer buffer;
    2. QImageWriter writer(&buffer, "PNG");
    3. writer.write( randomImage() );
    4. QByteArray data;
    5. QDataStream stream(&socket);
    6. stream << (quint32)buffer.data().size();
    7. stream << buffer.data();
    To copy to clipboard, switch view to plain text mode 
    works good as well.

    1 i am so confused, what is purpose of having QByteArray in first code???

    2 and in second code i don't even need socket.write( ) to send data... why>?

    3 in first code, dose "stream << (quint32)buffer.data().size();" means write the size into (QByteArray) data?
    Last edited by dognzhe; 7th May 2009 at 06:59.

  2. #2
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: how QDataStream and QByteArray related

    QByteArray is a container for a raw data.
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  3. #3
    Join Date
    Jan 2008
    Location
    Poland
    Posts
    687
    Thanks
    4
    Thanked 140 Times in 132 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: how QDataStream and QByteArray related

    I think the reason for use QByteArray could be here (Qt Docs for QIODevice::write()):
    Writes the content of byteArray to the device. Returns the number of bytes that were actually written, or -1 if an error occurred.
    so you can store your raw data, then TRY ti write it with write() function, and if it fails or wrote not all data, you already have raw data to resolve that problem. So i think it's good to check how many bytes write() did actualy write, and maybe try to write the missing data, which is easy when you have them stored as QByteArray.
    I would like to be a "Guru"

    Useful hints (try them before asking):
    1. Use Qt Assistant
    2. Search the forum

    If you haven't found solution yet then create new topic with smart question.

Similar Threads

  1. QDataStream and QByteArray issue
    By sepehr in forum Qt Programming
    Replies: 13
    Last Post: 2nd April 2021, 16:41
  2. Streaming QImage (QByteArray, QDataStream, QBuffer)
    By knarz in forum Qt Programming
    Replies: 5
    Last Post: 17th January 2009, 22:05
  3. QByteArray problem
    By Misenko in forum Qt Programming
    Replies: 17
    Last Post: 4th October 2008, 21:53
  4. QDataStream use
    By Doug Broadwell in forum Qt Programming
    Replies: 2
    Last Post: 21st July 2007, 22:17
  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.