[Qt-interest] How to capture stdout and redirect it to the gui?

K. Frank kfrank29.c at gmail.com
Thu Jan 27 15:16:52 CET 2011


Bob and Gabriel -

Thanks for the suggestions.

On Wed, Jan 26, 2011 at 10:21 PM, Bob Hood <bhood2 at comcast.net> wrote:
> On 1/26/2011 8:03 PM, K. Frank wrote:
>> Hello Bob!
>> ...
>> In my case, I don't have a separate process; instead,  I call an
>> in-process library (that writes to stdout, and that I would prefer
>> not to modify)
>> ...
> If it's a library function running from within your main process space, I'd
> guess that means that it's piggy backing off your application's stdout.  You
> should be able to redirect your own stdout/stderr to a file or other mechanism
> (like a pipe) before calling the library and then read the output from there.
> This doesn't necessarily involve Qt; you can manipulate your stdout/stderr
> file descriptors using just the standard C functions.
> ...

On Wed, Jan 26, 2011 at 10:42 PM, Gabriel M. Beddingfield
<gabrbedd at gmail.com> wrote:
> On Wednesday, January 26, 2011 09:03:28 pm K. Frank wrote:
>> In my case, I don't have a separate process; instead,  I
>> call an in-process library (that writes to stdout, and
>> that I would prefer not to modify).
>> ...
> freopen() will let you redirect your own stdout.  See
> http://www.cplusplus.com/reference/clibrary/cstdio/freopen/
>
> -gabriel

Just thinking out loud here...

Following your suggestions, perhaps I can freopen
stdout to a pipe.  Would I then set up a helper thread
to read the pipe, and stuff the output into the QTextEdit?

Ulrich's QDebugStream (for std::cout) manages to do
this synchronously (no helper thread), but I'm not seeing
how to avoid a thread with stdout and a pipe.

It's been a while since I've dabbled with c-style i/o.
We'll see...
- Hide quoted text -

Thanks for your help.


K. Frank



More information about the Qt-interest-old mailing list