PDA

View Full Version : Serialize a C++ string in QDatastream



Denarius
1st April 2009, 11:34
hey,
I was wondering how I could serialize a c++ string into a QDatastream

something like this:


string myString = "hello";
someStruct.myString = myString;


serialize via operator:


QDataStream& operator<< (QDataStream& out, const someStruct)
{
out << someStruct.myString;


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?

talk2amulya
1st April 2009, 11:43
wont this work for u?


QDataStream & QDataStream::operator<< ( const char * s )
This is an overloaded member function, provided for convenience.
Writes the '\0'-terminated string s to the stream and returns a reference to the stream.

Denarius
1st April 2009, 13:40
wont this work for u?


QDataStream & QDataStream::operator<< ( const char * s )
This is an overloaded member function, provided for convenience.
Writes the '\0'-terminated string s to the stream and returns a reference to the stream.

I tried this. After some more searching I realized my problem.
This is what I did:


string myString;
myString.c_str;


Instead of:


string myString;
myString.c_str();


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