Im currently using QProcess
to run Linux commands from a C++ app and I'm having a strange behavior when using QProcess::start
function.
This minimal example illustrates that even if the process fails, the process exit status is considered to be successful by QProcess::exitStatus()
even if QProcess::exitCode()
returns the good exit code.
int main(int argc, char *argv[])
{
QProcess process;
process.start("sleep a");
process.waitForFinished();
process.close();
if(QProcess::NormalExit == process.exitStatus())
{
qCritical() << "SUCCESS : " + QString::number(process.exitCode());
}
else
{
qCritical() << "ERROR : " + QString::number(process.exitCode());
}
}
Output
$ ./test
"SUCCESS : 1"
I found out that using Process::execute
instead of Process::start
doesn't change anything about Process::exitStatus
, it's even worse since Process::exitCode
doesn't returns 1 anymore.
int main(int argc, char *argv[])
{
QProcess process;
qCritical() << process.execute("sleep a");
if(QProcess::NormalExit == process.exitStatus())
{
qCritical() << "SUCCESS : " + QString::number(process.exitCode());
}
else
{
qCritical() << "ERROR : " + QString::number(process.exitCode());
}
}
Output
$ ./test
/bin/sleep: invalid time interval ‘a’
Try '/bin/sleep --help' for more information.
1
"SUCCESS : 0"
Both Process::exitStatus()
and Process::exitCode()
seems to fail to work correctly.
Does anybody knows why those function are not working ? Am I misusing them ?