异步检查 QProcess 是否正确启动
Asynchronous check if QProcess started correctly
我需要检查进程是否正确启动。
我在
类似的问题,但是我的有点不同。
同步
对于同步检查,我可以很容易地做这样的事情:
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 中引入。
问题
那么我如何执行 异步 检查 QProcess
是否在 Qt < 5.6 中正确启动?
根据文档,Qt 5.5 及更早版本中有一个信号 QProcess::error。
This signal is emitted when an error occurs with the process. The
specified error describes the type of error that occurred.
不,QProcess::error
正是您所需要的。它包含所有 information 以检查是否发生错误。
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
正是您所需要的。
我需要检查进程是否正确启动。
我在 类似的问题,但是我的有点不同。
同步
对于同步检查,我可以很容易地做这样的事情:
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 中引入。
问题
那么我如何执行 异步 检查 QProcess
是否在 Qt < 5.6 中正确启动?
根据文档,Qt 5.5 及更早版本中有一个信号 QProcess::error。
This signal is emitted when an error occurs with the process. The specified error describes the type of error that occurred.
不,QProcess::error
正是您所需要的。它包含所有 information 以检查是否发生错误。
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
正是您所需要的。