Results 1 to 7 of 7

Thread: QDataStream reading to C++ vectors

  1. #1
    Join Date
    Jan 2008
    Posts
    107
    Thanks
    36
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Question QDataStream reading to C++ vectors

    Hi,

    I have this vector defined:
    Qt Code:
    1. vector<char> payload;
    To copy to clipboard, switch view to plain text mode 
    and re-sized it to the appropriate size with:
    Qt Code:
    1. payload.resize(size_packet_data+2);
    To copy to clipboard, switch view to plain text mode 

    and some QDataStream object like 'mybytes'...

    and, as expected I can't do

    Qt Code:
    1. mybytes >> payload.
    To copy to clipboard, switch view to plain text mode 

    So, quick question is:
    What the best way to read (len) bytes from a QDataStream to a vector?

    TIA,
    Pedro.

  2. #2
    Join Date
    Jan 2006
    Location
    Napoli, Italy
    Posts
    621
    Thanks
    5
    Thanked 86 Times in 81 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QDataStream reading to C++ vectors

    You can write code like
    Qt Code:
    1. ...
    2. char ch;
    3. for (int i = 0; i < size_packet_data; ++i)
    4. {
    5. mybytes >> ch;
    6. payload.push_back(ch);
    7. }
    8. ...
    To copy to clipboard, switch view to plain text mode 
    A camel can go 14 days without drink,
    I can't!!!

  3. The following user says thank you to mcosta for this useful post:

    pdoria (23rd July 2009)

  4. #3
    Join Date
    Jan 2008
    Posts
    107
    Thanks
    36
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QDataStream reading to C++ vectors

    Thank you mcosta!

    First thing:

    It'd only compile if :
    Qt Code:
    1. mybytes >> (char *&) ch;
    To copy to clipboard, switch view to plain text mode 

    Secondly I'm wondering if there's a more optimized way to do it beyond a for...next loop

    TY,
    Pedro

  5. #4
    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: QDataStream reading to C++ vectors

    Quote Originally Posted by pdoria View Post
    and, as expected I can't do

    Qt Code:
    1. mybytes >> payload.
    To copy to clipboard, switch view to plain text mode 
    This semantics means "read a vector of items from the stream"

    The stream contains something else, I presume, so this will obviously fail.

    So, quick question is:
    What the best way to read (len) bytes from a QDataStream to a vector?
    Having a vector of chars is... well... strange. What you want is QByteArray. Then if you write a byte array to the stream at one end, you can read it on the other:
    Qt Code:
    1. QByteArray array;
    2. stream >> array;
    To copy to clipboard, switch view to plain text mode 

    Also note QDataStream is not a general purpose binary stream but a (de)serialization mechanism. So if you didn't write a byte array at one end, you can't read it on the other. For that you can use readRawData() or simply QFile API.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  6. The following user says thank you to wysota for this useful post:

    pdoria (23rd July 2009)

  7. #5
    Join Date
    Jan 2008
    Posts
    107
    Thanks
    36
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QDataStream reading to C++ vectors

    Thank you wysota! (as always to the rescue! )

    Let me expose my code:

    the structure:
    Qt Code:
    1. //! \brief Fleet Management Protocol
    2. //! \note All packets related to the fleet management protocol use packet ID 161.
    3. typedef struct {
    4. quint8 DLE; // =16;
    5. quint8 packet_id; // =161
    6. quint8 size_packet_data; // size of fleet_management_payload + 2
    7. quint16 fleet_management_packet_id; // see #defines below
    8. vector<char> fleet_management_payload; // 0 to 253 bytes
    9. quint8 checksum; // compute this BEFORE sending the packet!
    10. quint8 DLE_end; // =16
    11. quint8 ETX; // =3
    12. } fleet_management_packet_data_type;
    To copy to clipboard, switch view to plain text mode 

    the function (so far):
    Qt Code:
    1. //! \brief Process Fleet Management packets
    2. //! \param records QDataStream raw data
    3. //! \note First two bytes of the packet have already been read by TeltonikaUnit::parseFM4GarminRecs
    4. void processFleetManagement ( QDataStream & records ) {
    5. fleet_management_packet_data_type packet;
    6. bool debug = true;
    7.  
    8. records >> packet.size_packet_data;
    9. records >> packet.fleet_management_packet_id;
    10. packet.fleet_management_payload.resize(packet.size_packet_data+2);
    11.  
    12. // get the payload in a byte at a time...
    13. char ch;
    14. for (int i = 0; i < packet.size_packet_data; ++i)
    15. {
    16. records >> (char*&) ch;
    17. packet.fleet_management_payload.push_back(ch);
    18. }
    19. records >> packet.checksum;
    20. records >> packet.DLE_end;
    21. records >> packet.ETX;
    22.  
    23. }
    To copy to clipboard, switch view to plain text mode 

    So, as you can see, I'm trying to read packet.size_packet_data bytes from the data stream.
    Your suggestion for using a QByteArray is perfectly valid but I'd like to stick with a vector, hence the question in the first place...

    TIA,
    Pedro.

  8. #6
    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: QDataStream reading to C++ vectors

    How was the data written to the device the DataStream operates on?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  9. #7
    Join Date
    Jan 2006
    Location
    Napoli, Italy
    Posts
    621
    Thanks
    5
    Thanked 86 Times in 81 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QDataStream reading to C++ vectors

    You can use

    Qt Code:
    1. mybytes >> (qint8) ch;
    To copy to clipboard, switch view to plain text mode 
    A camel can go 14 days without drink,
    I can't!!!

Similar Threads

  1. QFile, QDataStream reading binary data
    By yren in forum Qt Programming
    Replies: 1
    Last Post: 15th April 2009, 06:34
  2. QWT 5, QT3, SuSE 10.2. Crash and burn
    By DrMcCleod in forum Qwt
    Replies: 8
    Last Post: 7th September 2007, 20:53
  3. Replies: 8
    Last Post: 27th August 2007, 15:45
  4. QDataStream reading into QString
    By jakamph in forum Qt Programming
    Replies: 4
    Last Post: 15th October 2006, 09:22
  5. Reading QByteArray from QDataStream Qt3.3
    By high_flyer in forum Qt Programming
    Replies: 2
    Last Post: 1st April 2006, 20:23

Tags for this Thread

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.