Read Qprocess Output to String

read QProcess output to string

Did you try QByteArray QProcess::readAllStandardOutput() docs - here

QString can be instantiated from QByteArray:

QString output(pingProcess.readAllStandardOutput());

As others mentioned, and I join to them, you should not use execute method and replace it with:

pingProcess.start(exec, params);
pingProcess.waitForFinished(); // sets current thread to sleep and waits for pingProcess end
QString output(pingProcess.readAllStandardOutput());

Using QProcess to read standard output

As I said in my comments one of the main problems is that when you run tutorial3 that process is separated so you can not get the output. Therefore, I recommend executing it directly, and QProcess is probably a local variable, eliminating after printing an empty text, a possible solution is to create a pointer. Another improvement would be to use the readyReadStandardOutput and readyReadStandardError signals since the impressions are not automatic.

QProcess *process = new QProcess(this);

connect(process, &QProcess::readyReadStandardOutput, [process, this](){
QString output =process->readAllStandardOutput();
qDebug() << "output: "<< output;
});

connect(process, &QProcess::readyReadStandardError, [process](){
QString err = process->readAllStandardError();
qDebug() << "error: "<<err;
});

process->setWorkingDirectory("/home/pi/Desktop/tutorial3App/bin/")
process->start("tutorial3", QStringList() << "start");

How to get STDOUT from a QProcess?

Before starting your process call:

process.setProcessChannelMode(QProcess::MergedChannels);

It will cause printing everything (even STDERR output) to STDOUT output.

How to read when QProcess need user input with Qt

My test.bat

set /p answer=Do you want to create it now (Y/N)?
echo "user response:" %answer%
pause

The code starts the batch command, reads it output and writes the answer to the process:

QProcess *compilator = new QProcess(this);

connect(compilator, &QProcess::readyReadStandardOutput, [compilator, this]()
{
QString output = compilator->readAllStandardOutput().simplified();
qDebug().noquote() << "output: " << output;

if (output.contains("(Y/N)?", Qt::CaseInsensitive))
{
compilator->write("Y\n"); // write our answer to the process
}
else if (output.contains(". . .", Qt::CaseInsensitive))
{
compilator->write("\n"); // simulate press any key to process
}
});

connect(compilator, &QProcess::readyReadStandardError, [compilator, this]()
{
QString err = compilator->readAllStandardError().simplified();
qWarning().noquote() << "error: " << err;
});

// Handle the finished() signal:
connect(compilator, QOverload<int, QProcess::ExitStatus>::of(&QProcess::finished),
[compilator, this](int exitCode, QProcess::ExitStatus exitStatus)
{
qDebug() << "compilator process finished with exit code =" << exitCode
<< "and exit status =" << exitStatus;
});

compilator->start("test.bat");

if (compilator->waitForStarted()) // use to check start errors
{
qDebug() << "compilator process started ok";
}
else
{
qCritical() << "compilator process start FAILED:" << compilator->errorString();
}


Related Topics



Leave a reply



Submit