+ Reply to Thread
Results 1 to 3 of 3

Thread: QDataStream >> QString

  1. #1
    Join Date
    Jan 2006
    Posts
    18
    Qt products
    Qt4
    Platforms
    Windows

    Default QDataStream >> QString

    hi all,

    i was struggling with putting a qstring into a qdatastream and getting it out again.
    reading through the 4.0 docu's fortune client example it seemed to me that a QString str, when written to a QDataStream, needed str.length bytes.
    but this is wrong. it should be 2*length + 4 (every letter in the string gets encoded with 2 bytes and the strings length goes at the beginning encoded with a 4 bytes int) right?

    and another thing (which is actually my question) is the following:

    Qt Code:
    1. // sender
    2. QDataStream stream(socket);
    3. stream << some_QString;
    4. stream << some_quint;
    5.  
    6. // receiver
    7. QDataStream stream(socket);
    8. stream >> some_QString;
    To copy to clipboard, switch view to plain text mode 

    i found out that in my case the receiver gets an exact copy of some_QString and that the following integer is not read. which makes sence after finding out that QString is serialized such that it carries its own length with it. but can i count on that? i'm sending strings followed by ints. so can i just read the strings like that:

    Qt Code:
    1. stream >> some_QString;
    To copy to clipboard, switch view to plain text mode 

    when i know it is followed by an int. or should i still take care by myself that i dont mix up string and int??

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    31,806
    Thanks
    3
    Thanked 4,772 Times in 4,570 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Blog Entries
    4
    Wiki edits
    10

    Default Re: QDataStream >> QString

    If you write to a descriptor using QDataStream, you can safely read from it using a QDataStream. The problem is only if you write using a QDataStream and read without using it or vice versa.

  3. #3
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    85
    Thanks
    1
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QDataStream >> QString

    The idea behind QDataStream is that you can serialize anything with it one time and later deserialize it . So what you put in the stream you have to read back in the same order: No extra counter needed.

    The correct receiver for your sender would be:
    Qt Code:
    1. stream >> some_QString;
    2. stream >> some_quint;
    To copy to clipboard, switch view to plain text mode 

    What is not guaranteed is how your data is stored - only that you get later an exact copy back.

+ Reply to Thread

Similar Threads

  1. Custom Model Advice Requested
    By mclark in forum Qt Programming
    Replies: 3
    Last Post: 18th September 2008, 17:26
  2. QString static callback function from CURL
    By tpf80 in forum Qt Programming
    Replies: 12
    Last Post: 16th May 2007, 21:47
  3. Convert from iso-8859-1 to... Something else :-)
    By Nyphel in forum Qt Programming
    Replies: 4
    Last Post: 7th March 2007, 18:59
  4. QDataStream reading into QString
    By jakamph in forum Qt Programming
    Replies: 4
    Last Post: 15th October 2006, 10:22
  5. [SOLVED] Widget plugin ... how to ?
    By yellowmat in forum Newbie
    Replies: 10
    Last Post: 29th January 2006, 21:41

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