我需要检查进程是否正确启动。
我在类似的问题,但是我的有点不同。
同步
对于同步检查,我可以轻松地执行以下操作:
QProcess process("foo.exe");
if (!process.waitForStarted()) {
qWarning() << process.errorString();
}
异步
对于异步检查,我可以这样做:
QProcess *process = new QProcess("foo.exe");
connect(process, &QProcess::errorOccurred, [=]() {
qWarning() << process->errorString();
});
但是,该QProcess::errorOccurred
仅在Qt 5.6中引入。
问题
那么,如何在Qt <5.6中正确启动QProcess
进行异步检查呢?
根据文档,Qt 5.5 及更早版本中有一个信号 QProcess::error。
当进程发生错误时,将发出此信号。这 指定的错误描述发生的错误类型。
不,QProcess::error
是您需要的。它包含检查是否发生错误的所有信息。
QProcess::FailedToStart 0 The process failed to start. Either the invoked program is missing, or you may have insufficient permissions to invoke the program.
QProcess::Crashed 1 The process crashed some time after starting successfully.
QProcess::Timedout 2 The last waitFor...() function timed out. The state of QProcess is unchanged, and you can try calling waitFor...() again.
QProcess::WriteError 4 An error occurred when attempting to write to the process. For example, the process may not be running, or it may have closed its input channel.
QProcess::ReadError 3 An error occurred when attempting to read from the process. For example, the process may not be running.
QProcess::UnknownError 5 An unknown error occurred. This is the default return value of error().
异步检查,Qt 5.5 及更早
版本connect(process, static_cast<void(QProcess::*)(QProcess::ProcessError)>(&QProcess::error),
[=](QProcess::ProcessError error){ if(error == QProcess::FailedToStart) qDebug() << "Process failed to start"; });
QProcess::error
完全满足您的需求。