Guerrette
Guerrette

Reputation: 67

QProcess returning an exit code of 0

I'm using QProcess to run a ping to a few hosts. When I use QProcess with the command as "ping -c 1 -W 1 hostname" the exit code being returned is always 0 regardless if the ping is successful or not. I have tested hosts that are offline in the terminal and the exit code is as expected. Assume hostname is an actual host that is offline. This is the code snippet that includes QProcess:

QProcess *process = new QProcess(this);
QString cmd("ping -c 1- W 1 hostname");
process->start(cmd);
std::cout << process->exitCode() << std::endl;

Upvotes: 1

Views: 3583

Answers (1)

covener
covener

Reputation: 17896

QProcess::exitCode() says:

Returns the exit code of the last process that finished.

So, you need to wait for the process (QProcess::waitForFinished) to exit before checking its exit code.

Example:

if ( !process->waitForFinished( -1 ) )
{
    qWarning() << "Error:" << process->errorString();
    return;
}

qDebug() << "Exit code:" << process->exitCode();

Upvotes: 3

Related Questions