How to get STDOUT from a QProcess?
Solution 1
Before starting your process call:
process.setProcessChannelMode(QProcess::MergedChannels);
It will cause printing everything (even STDERR output) to STDOUT output.
Solution 2
Here is some clarification:
According to http://doc.qt.io/qt-5/qprocess.html#ProcessChannelMode-enum,
-
QProcess::MergedChannels
:QProcess
merges the output of the running process into the standard output channel (stdout). The standard error channel (stderr) will not receive any data.[...]
but
-
QProcess::ForwardedChannels
:QProcess
forwards the output of the running process onto the main process. Anything the child process writes to its standard output and standard error will be written to the standard output and standard error of the main process.
Related videos on Youtube
Nathan Osman
Email: [email protected] By profession, I am a software developer and I work with C++, Python, and (more recently) Go. Here are some of my recent projects: - Hectane - lightweight SMTP server written in Go - NitroShare - a cross-platform network file transfer utility - REST Easy - Firefox add-on for analyzing HTTP responses
Updated on July 09, 2022Comments
-
Nathan Osman almost 2 years
I thought I was going to get the output from a QProcess using the following code:
// Start the process process.start(tr("php-cgi www/test.php"),QIODevice::ReadWrite); // Wait for it to start if(!process.waitForStarted()) return 0; // Continue reading the data until EOF reached QByteArray data; while(process.waitForReadyRead()) data.append(process.readAll()); // Output the data qDebug(data.data()); qDebug("Done!");
What I am expecting is to see the output from the program printed to the debug console, but all I see is:
Done!
I know that:
- The program is started fine, because the message at the end is printed.
- The program does print output because running the exact same command in the terminal produces a long string of text as expected.
What am I doing wrong here?
-
Valentin H about 11 yearsThere is no point enclosing executable with language translation macro tr(). I also doubt, that putting executable and parameter as first argument would work. Better: process.start("php-cgi", QStringList()<<"www/test.php",QIODevice::ReadWrite);
-
C-- over 5 yearsProcessing the main process's output is slightly easier. So, thanks.