0

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?

demonplus
  • 5,613
  • 12
  • 49
  • 68
APU
  • 239
  • 2
  • 17

1 Answers1

1

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.

James Turner
  • 2,425
  • 2
  • 19
  • 24
  • Your answer gives me a hint, I found a function under QProcess call "execute", which is I want. It will block the process until it is done. – APU Nov 20 '15 at 06:53
  • My understanding is that start + waitForFinished will do the same, but I haven't actually tested that. Glad you have something that works for your needs, anyway. – James Turner Nov 20 '15 at 15:24