June 21, 2012

devlikeme devlikeme
Lab Rat
5 posts

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

8 replies

June 22, 2012

Volker Volker
Robot Herder
5428 posts

If you have an ostream >> operator complementing the other one, that should work. I don’t get the rationale behind the question, to be honest. What’s your actual problem here?

June 25, 2012

devlikeme devlikeme
Lab Rat
5 posts

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 [doc.qt.nokia.com] as I was searching for a : QDataStream & operator<< ( ostream )

May be you could tell/explain it me ?

thanks

June 26, 2012

devlikeme devlikeme
Lab Rat
5 posts

Hi Everyone

my question is still open, is it possible to create a QDataStream & operator<< ( ostream ) ?

Thank you

June 26, 2012

Andre Andre
Area 51 Engineer
6031 posts

Yes, I think you need to create a separate

  1. friend QDataStream& operator<<(const MyObject& object);

method. I don’t see a way to generically create a
  1. 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?

 Signature 

Looking for Qt developers to join our team @ i-Optics: https://qt-project.org/forums/viewthread/25393/

June 26, 2012

devlikeme devlikeme
Lab Rat
5 posts

Thanks for your response. I’d be interested in an exemple if anyone would give one.

June 26, 2012

Andre Andre
Area 51 Engineer
6031 posts

Well, a little bit of Googling turned up std::stringstream. If you use that stream, you have access to a string, which you should be able to stream to a QDataStream again.

 Signature 

Looking for Qt developers to join our team @ i-Optics: https://qt-project.org/forums/viewthread/25393/

June 26, 2012

devlikeme devlikeme
Lab Rat
5 posts

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 :)

June 26, 2012

koahnig koahnig
Mad Scientist
2110 posts

devlikeme wrote:
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 :)


No, you do not have to make so complicated.
Your ostream & operator<< can be used directly with an ostringstream. E.g.
  1. #include <sstream>   // required for ostringstream
  2.  
  3. void foo ()
  4. {
  5.     std :: ostringstream ostr;
  6.     int nr = 10;
  7.     ostr << nr;    // streams 10 to ostr;
  8.     string str = ostr.str();    // copies the ostringstream to a string
  9. }

 
  ‹‹ qmake not loading qt.conf      [Solved]Qt Installation with MSVC or MinGW ››

You must log in to post a reply. Not a member yet? Register here!