开发者

QTextStream and std out

I have the code:

QTextStream out(mFileHandle);

out << (QTime::currentTime().toString(Qt::LocalDate) + " - ").toAscii();
out << "Something another";

std::cout << "Data: \n";
std::c开发者_开发知识库out << out.string();
out.flush();

It writes to file but after 'Data:' I get '0', why? How to send data to both streams?


It looks to me like you constructed your QTextStream out from a file handle. This means that it is going to write to that file.

The .string() method in QTextStream is only to access the QString used to construct it. In this case, it is zero, since you didn't use a string to construct it.

If you want to access the all the text that has been written to the QTextstream, you probably want to pass it a QByteArray as the constructor argument. This will make it write output to that QByteArray, rather than out to a file. Then, you can access the contents written to the stream through the QByteArray.


You would do somthing like that:

QByteArray outBytes;
QTextStream outStream(&outBytes);

//write something to outBytes via stream
outStream << (QTime::currentTime().toString(Qt::LocalDate) + " - ");
outStream << "Something another";

//construct QString from written bytes
std::cout << QString(outBytes).toStdString(); 
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜