Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Installation and Deployment
  4. Using << ostream to a QDataStream, is it possible ?
QtWS25 Last Chance

Using << ostream to a QDataStream, is it possible ?

Scheduled Pinned Locked Moved Installation and Deployment
9 Posts 4 Posters 8.6k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    devlikeme
    wrote on last edited by
    #1

    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

    1 Reply Last reply
    0
    • G Offline
      G Offline
      goetz
      wrote on last edited by
      #2

      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?

      http://www.catb.org/~esr/faqs/smart-questions.html

      1 Reply Last reply
      0
      • D Offline
        D Offline
        devlikeme
        wrote on last edited by
        #3

        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

        1 Reply Last reply
        0
        • D Offline
          D Offline
          devlikeme
          wrote on last edited by
          #4

          Hi Everyone

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

          Thank you

          1 Reply Last reply
          0
          • A Offline
            A Offline
            andre
            wrote on last edited by
            #5

            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?

            1 Reply Last reply
            0
            • D Offline
              D Offline
              devlikeme
              wrote on last edited by
              #6

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

              1 Reply Last reply
              0
              • A Offline
                A Offline
                andre
                wrote on last edited by
                #7

                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.

                1 Reply Last reply
                0
                • D Offline
                  D Offline
                  devlikeme
                  wrote on last edited by
                  #8

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

                  1 Reply Last reply
                  0
                  • K Offline
                    K Offline
                    koahnig
                    wrote on last edited by
                    #9

                    [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
                    }
                    @

                    Vote the answer(s) that helped you to solve your issue(s)

                    1 Reply Last reply
                    0

                    • Login

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • Users
                    • Groups
                    • Search
                    • Get Qt Extensions
                    • Unsolved