PDA

View Full Version : QIODevice conversion to / from std::i(o)stream



TorAn
29th January 2012, 18:44
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:


std::ostream& operator << (std::ostream& s, const myClass& a)
{
int i = 5;
s << i;
QIODevice device; // somehow wrap stream "s" to be QIODevice instance.
QXmlStreamWriter writer(&device);
writer.writeStartDocument("1.0");
..
return s;
}

ChrisW67
30th January 2012, 03:29
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()?

TorAn
30th January 2012, 03:33
ChrisW67, thanks, that is similar to what I am doing now. But it would be nice to have smth like
QIODeviceWrapper dw (std:ostream);
that would handle all internals :).

wysota
30th January 2012, 11:19
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.