Using << ostream to a QDataStream, is it possible ?
-
Hi everyone,
I'm building an application that needs to save an object into a file. I've already used QDataStream to save Qt Object into files, but my project is a little different, i want to keep the Gui appart.
I've search on the web didn't find any solution so far. Am I compelled to add a friend QDataStream operator<< for my object or is there a way to use the friend ostream operator<< which is already defined ?
In advance thank you for your help
-
Thank you for your response.
English is a foreign language for me so may be i was not clear enough.
I have a class width a method "friend ostream operator<<". I was wondering if i had to create a "friend QDataStream operator<<" too.
Reading the "documentation":http://doc.qt.nokia.com/4.7-snapshot/qdatastream.html as I was searching for a : QDataStream & operator<< ( ostream )
May be you could tell/explain it me ?
thanks
-
Yes, I think you need to create a separate
@
friend QDataStream& operator<<(const MyObject& object);
@
method. I don't see a way to generically create a
@
QDataStream& operator<<(ostream);
@
Then again, I don't know too much about the details of ostream, so I might be off. In any case, you'd have to create your ostream on some kind of buffer, so perhaps you could simply stream in that actual buffer with QDataStream instead of the ostream itself? -
Thanks to your advice, I began to see how to do ...
My understanding is the following, I have a friend ostream& operator<<() in a class. I use a QdataStream << operator in my Qt Application. So I need a buffer for my ostream to send that buffer to the QDataStream.
My first problem being buffering my ostream, thanks to google, i think but i'm not sure that rdbuf is my solution.
My second problem is what can I do with my buffer, again with no certainty, i believe that using streambuf::sgetc I might be able to create a QDataStream using the QDataStream & operator<< ( const char * s )
Google lost me my friend :( I really need a "how to" on this one because i'm lost between the documentations of C++ reference and Qt. They are sometimes hard to understand. I'm going to try what i explainded. I'll post my code if I succeed :)
-
[quote author="devlikeme" date="1340723379"]Thanks to your advice, I began to see how to do ...
My understanding is the following, I have a friend ostream& operator<<() in a class. I use a QdataStream << operator in my Qt Application. So I need a buffer for my ostream to send that buffer to the QDataStream.
My first problem being buffering my ostream, thanks to google, i think but i'm not sure that rdbuf is my solution.
My second problem is what can I do with my buffer, again with no certainty, i believe that using streambuf::sgetc I might be able to create a QDataStream using the QDataStream & operator<< ( const char * s )
Google lost me my friend :( I really need a "how to" on this one because i'm lost between the documentations of C++ reference and Qt. They are sometimes hard to understand. I'm going to try what i explainded. I'll post my code if I succeed :)
[/quote]
No, you do not have to make so complicated.
Your ostream & operator<< can be used directly with an ostringstream. E.g.
@
#include <sstream> // required for ostringstreamvoid foo ()
{
std :: ostringstream ostr;
int nr = 10;
ostr << nr; // streams 10 to ostr;
string str = ostr.str(); // copies the ostringstream to a string
}
@