PDA

View Full Version : QTextStream with QString



kiker99
3rd July 2006, 18:09
Hello,

i don't quite understand how QTextStream works together with QStrings. I've got this code:



QString s;
QTextStream ts(&s);
ts<<11<<" Freunde";
std::cout<<"1."<<ts.readAll().toStdString()<<'\n'<<"2."<<s.toStdString();


That produces this output:
1.11 Freunde

Why doesn't QTextStream write to the String, as intended? I would be happy with this behaviour, too, but why do I have to specify a string as an argument with the constructor? Is there a function that needs to be called? I didn't find one and in the examples it works without one - but the examples work with devices rather than QStrings.
Thanks.

munna
3rd July 2006, 18:23
This (http://doc.trolltech.com/4.1/qtextstream.html#details) might help to understand things better.

kiker99
3rd July 2006, 18:40
This (http://doc.trolltech.com/4.1/qtextstream.html#details) might help to understand things better.

The Qt Documentation is my first ressource if I want to look up something, so I've read that already. I don't see an answer to my question in there.

jpn
3rd July 2006, 19:54
Try calling QTextStream::flush() to flush the buffered data waiting to be written.

gfunk
3rd July 2006, 20:03
That's weird, when I run it, the output I get is:

1.11 Freunde
2.11 Freunde

which is what I expect. (Running WinXP, Qt 4.1.3)
The docs say that QTextStream::flush() has no effect when QTextStream operates on a string... Maybe your cout just needs to be flushed? weird.

kiker99
3rd July 2006, 23:36
Thanks, but neither



QString s;
QTextStream ts(&s);
ts<<11<<" Freunde";
ts.flush();
std::cout<<"1."<<ts.readAll().toStdString()<<'\n'<<"2."<<s.toStdString();


nor



QString s;
QTextStream ts(&s);
ts<<11<<" Freunde";
std::cout<<"1."<<ts.readAll().toStdString()<<'\n'<<"2."<<s.toStdString();
std::cout.flush();


nor a combination ob both helps. But perhaps we are on the right track :rolleyes:
I'm using Qt 4.1.4 on Linux.

gfunk
3rd July 2006, 23:41
Why "2." doesn't even show in your output?
What happens if you split the cout into two lines?

std::cout<<"1."<<ts.readAll().toStdString()<<'\n';
std::cout <<"2."<<s.toStdString();

jpn
4th July 2006, 07:11
What about if you try to use qDebug() for outputting?



#include <QtDebug>
qDebug() <<"1."<<ts.readAll()<<'\n'<<"2."<<s;