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



  • 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?



  • 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



  • Hi Everyone

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

    Thank you



  • 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 for your response. I'd be interested in an exemple if anyone would give one.



  • 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.



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


  • Moderators

    [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 ostringstream

    void foo ()
    {
    std :: ostringstream ostr;
    int nr = 10;
    ostr << nr; // streams 10 to ostr;
    string str = ostr.str(); // copies the ostringstream to a string
    }
    @


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.