Results 1 to 4 of 4

Thread: QIODevice conversion to / from std::i(o)stream

  1. #1
    Join Date
    Nov 2009
    Location
    US, Midwest
    Posts
    215
    Thanks
    62
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Unhappy QIODevice conversion to / from std::i(o)stream

    I have to use QIODevice in overloaded operators << and >> where std::i(o)streams are passed-in as arguments.

    I'll appreciate the advice (or code ) for the wrapper that will allow me to initialize QIODevice instance with standard streams.
    Example of what I am trying to accomplish:

    Qt Code:
    1. std::ostream& operator << (std::ostream& s, const myClass& a)
    2. {
    3. int i = 5;
    4. s << i;
    5. QIODevice device; // somehow wrap stream "s" to be QIODevice instance.
    6. QXmlStreamWriter writer(&device);
    7. writer.writeStartDocument("1.0");
    8. ..
    9. return s;
    10. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: QIODevice conversion to / from std::i(o)stream

    Why not just stream your XML into a QBuffer and then access the raw character buffer of the internal QByteArray to send it to the std::ostream using write()?

  3. The following user says thank you to ChrisW67 for this useful post:

    TorAn (30th January 2012)

  4. #3
    Join Date
    Nov 2009
    Location
    US, Midwest
    Posts
    215
    Thanks
    62
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QIODevice conversion to / from std::i(o)stream

    ChrisW67, thanks, that is similar to what I am doing now. But it would be nice to have smth like
    QIODeviceWrapper dw (stdstream);
    that would handle all internals .

  5. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QIODevice conversion to / from std::i(o)stream

    You can make your wrapper do exactly what Chris suggests. Writing to an ostream is easy, reading from an istream is harder since the latter doesn't provide any notification upon receiving data.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. GUnzipDevice - QIODevice
    By zaphod77 in forum Qt-based Software
    Replies: 0
    Last Post: 1st April 2011, 21:22
  2. QIODevice and QTextCodec?
    By whitefurrows in forum Qt Programming
    Replies: 5
    Last Post: 26th November 2010, 18:50
  3. Video Stream, extending QIODevice
    By rextr in forum Qt Programming
    Replies: 3
    Last Post: 27th June 2008, 08:55
  4. QIODevice
    By sabeesh in forum Newbie
    Replies: 1
    Last Post: 26th September 2007, 10:01
  5. QIODEvice
    By mickey in forum Qt Programming
    Replies: 2
    Last Post: 5th July 2006, 19:00

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.