John Doe
John Doe

Reputation: 635

Asynchronous check if QProcess started correctly

I need to check if the process started correcly.

I've found this answer on the similar question, however mine is a bit different.

Synchronous

For synchronous check I could easily do something like this:

QProcess process("foo.exe");
if (!process.waitForStarted()) {
    qWarning() << process.errorString();
}

Asynchronous

For asynchronous check I could do this:

QProcess *process = new QProcess("foo.exe");
connect(process, &QProcess::errorOccurred, [=]() { 
    qWarning() << process->errorString();
});

However, the QProcess::errorOccurred was introduced only in Qt 5.6.

Question

So how do I perform an asynchronous check if the QProcess started correctly in Qt < 5.6?

Upvotes: 1

Views: 1338

Answers (1)

Michael Schm.
Michael Schm.

Reputation: 2534

According to the documentation there is a signal QProcess::error in Qt 5.5 and earlier.

This signal is emitted when an error occurs with the process. The specified error describes the type of error that occurred.

No, the QProcess::error is what you need. It contains all information to check if an error occurred.

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().

Asynchronous check, Qt 5.5 and earlier

connect(process, static_cast<void(QProcess::*)(QProcess::ProcessError)>(&QProcess::error),
    [=](QProcess::ProcessError error){ if(error == QProcess::FailedToStart) qDebug() << "Process failed to start"; });

QProcess::error does exactly what you need.

Upvotes: 2

Related Questions