PDA

View Full Version : Rerouting std::cout to a QPlainTextEdit



lynchkp
25th May 2011, 17:33
Hello,

I have a bit of an odd question, but I am wondering if it is possible to route the std::cout output into a QPlainTextEdit widget. I'm trying to provide a GUI application with textual feedback on the processing it is doing, but I am using some libraries that output to std::cout and thus the information they return is being lost.

Any suggestions would be appreciated!

Santosh Reddy
25th May 2011, 19:30
Yes, it is possible, you need change the cout objects's stream buffer, and then interface this stream buffer with QPlainTextEdit, that should be fairly simple.

have look at this example, how to get or set stream buffer

http://www.cplusplus.com/reference/iostream/ios/rdbuf/

wannabe
12th September 2013, 01:23
Hi.
I'm working on a plugin for a 3rd party Qt application.
I can access the qApp and I also have a pointer to the QMainWindow of this application.
I successfully managed to redirect stdout to a file by using this code:

freopen("C:\output.txt", "w", stdout);

But how can I redirect stdout to a QTextEdit or QPlainTextEdit widget?
I tried to do it using the QDebugStream method but it only printed to the QPlainTextEdit when I was explicitly calling cout<<"test"<<endl;
The QApplication itself still continued printing into the console window.

I also thought about reimplementing qInstallMsgHandler but somebody told me this is only for qDebug and not for stdout.
Maybe this can be done using QTextStream?
By the way I'm using Windows.
Thanks for any help!

wysota
12th September 2013, 06:54
Did you try what Santosh Reddy suggested?

wannabe
12th September 2013, 07:11
You are right. Santosh Reddy has the answer.
Thanks Santosh

wysota
12th September 2013, 08:23
Then look again at what Santosh posted. Then read it, understand it and implement it.