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

Bob Hood bhood2 at comcast.net
Thu Jan 27 01:55:02 CET 2011


On 1/26/2011 3:42 PM, K. Frank wrote:
> I would like to "capture" stdout and redirect it to a logging
> window, for example, a QTextEdit.
> ...
> Would someone know of a solution or have suggestions about
> things to try?
>

Here's an excerpt of code I use to capture and process the output of a
sub-process.  This should do what you're asking.  Note that I also merge the
channels, so stdout and stderr come in through stdout:

    ...
    process = new QProcess();
    process->setProcessChannelMode(QProcess::MergedChannels);

    if(working_path.size())
        process->setWorkingDirectory(working_path);

    connect(process, SIGNAL(finished(int)), this,
SLOT(slot_process_finished(int)));

    if(capture)
    {
        capture_elapsed = std::numeric_limits<int>::max();
        capture_timer.start();
        capture_called = false;
        connect(process, SIGNAL(readyReadStandardOutput()), this,
SLOT(slot_read_output()));
    }

    process->start(command, args);
    ...

So, in your method to read the output (e.g., "slot_read_output" above), you
just read what's currently available and then post it to your widget.




More information about the Qt-interest-old mailing list