Results 1 to 3 of 3

Thread: Serialize a C++ string in QDatastream

  1. #1
    Join Date
    Feb 2009
    Posts
    23
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Serialize a C++ string in QDatastream

    hey,
    I was wondering how I could serialize a c++ string into a QDatastream

    something like this:
    Qt Code:
    1. string myString = "hello";
    2. someStruct.myString = myString;
    To copy to clipboard, switch view to plain text mode 

    serialize via operator:
    Qt Code:
    1. QDataStream& operator<< (QDataStream& out, const someStruct)
    2. {
    3. out << someStruct.myString;
    To copy to clipboard, switch view to plain text mode 

    How do I serialize the string in the QDataStream.
    Obviously the above doesn't work.
    I need this for something but the Qt reference isn't helping me.

    Does anyone know how to do this?

  2. #2
    Join Date
    Feb 2009
    Location
    Noida, India
    Posts
    517
    Thanks
    21
    Thanked 66 Times in 62 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Serialize a C++ string in QDatastream

    wont this work for u?

    Qt Code:
    1. QDataStream & QDataStream::operator<< ( const char * s )
    2. This is an overloaded member function, provided for convenience.
    3. Writes the '\0'-terminated string s to the stream and returns a reference to the stream.
    To copy to clipboard, switch view to plain text mode 

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

    Denarius (1st April 2009)

  4. #3
    Join Date
    Feb 2009
    Posts
    23
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Serialize a C++ string in QDatastream

    Quote Originally Posted by talk2amulya View Post
    wont this work for u?

    Qt Code:
    1. QDataStream & QDataStream::operator<< ( const char * s )
    2. This is an overloaded member function, provided for convenience.
    3. Writes the '\0'-terminated string s to the stream and returns a reference to the stream.
    To copy to clipboard, switch view to plain text mode 
    I tried this. After some more searching I realized my problem.
    This is what I did:
    Qt Code:
    1. string myString;
    2. myString.c_str;
    To copy to clipboard, switch view to plain text mode 

    Instead of:
    Qt Code:
    1. string myString;
    2. myString.c_str();
    To copy to clipboard, switch view to plain text mode 

    So now it works. But thanks for the tip. It made me search a little harder.

Similar Threads

  1. Replies: 0
    Last Post: 18th February 2009, 13:31
  2. Reading from sockets in a multithreaded program
    By KoosKoets in forum Qt Programming
    Replies: 9
    Last Post: 4th April 2007, 20:43
  3. saving a c string of variable length in a shared memory?
    By nass in forum General Programming
    Replies: 4
    Last Post: 3rd January 2007, 14:40
  4. QDataStream reading into QString
    By jakamph in forum Qt Programming
    Replies: 4
    Last Post: 15th October 2006, 09:22
  5. Replies: 13
    Last Post: 19th May 2006, 02:10

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.