PDA

View Full Version : Redirect Cout from Static Library Calls running in QThread



lynchkp
21st February 2014, 15:53
Hello all,

I'm working on a program which makes many lengthy calls to an external library, which typically prints a large amount of diagnostic information to cout. I'd like to call this library from a separate QThread so my GUI remains responsive, but I would also like to redirect the cout to a QPlainTextEdit for the user to see.

I have looked into initializing a QDebugStream for the QThread to redirect the cout to the GUI, but it doesn't seem to suit me as it modifies the QPlainTextEdit directly via a pointer, and the GUI cannot be modified by a QThread except by signals.

Is there a standard way to do this that I have overlooked in my Google/Stackoverflow searches?

Regards,
Kyle

anda_skoa
21st February 2014, 17:33
The standard input/output/error streams are shared resources, i.e. all threads use the same ones.

So this is not a question of threading at all.

If you only want to redirect output of that library, my suggestion would be to create a helper program that runs that operation.
You can then run this program with QProcess and handle its output however you'd like.

Also solves the "keeping UI responsive" task.

Cheers,
_