0
votes

I was planning using QProcess to execute a program(.exe) in my computer and process a file which already exists, then output a new file and continue the next step, the whole process takes about 3 to 5 seconds.

However, although the process screen that doing the process does show up and running, and I also wrote lines of code to detect if the process has done, then do the next step, but yet can't stop the project from doing the following steps without waiting.

In other words, my project will try to open a file that isn't exist because it is still in process in the previous code.

So I wrote a simple code to test:

QProcess *proc = new QProcess(this);
ui->textEdit->append(QString("%1").arg(proc->state()));
ui->textEdit->append(QString("%1").arg(proc->exitCode()));
ui->textEdit->append(QString("%1").arg(proc->waitForStarted()));
ui->textEdit->append(QString("%1").arg(proc->waitForFinished()));
proc->startDetached("cmd");
ui->textEdit->append(QString("%1").arg(proc->state()));
ui->textEdit->append(QString("%1").arg(proc->exitCode()));
ui->textEdit->append(QString("%1").arg(proc->waitForStarted()));
ui->textEdit->append(QString("%1").arg(proc->waitForFinished()));

and the result was all "zero".

0
0
0
0
0
0
0
0

but the cmd.exe console is right there, cmd

am I misunderstood about QProcess's exitcode functions?

1

1 Answers

1
votes

The problem is you're mixing the 'fire and forget' API startDetached with the blocking (non-event loop) waitFor methods which work on a QProcess instance.

If you want to write blocking code, use start() to start the process running, not the detached version.

Of course for production code you probably want to do use signals and an event-loop to avoid blocking at all, but that's not the particular problem here.