Discussion:
[Development] Stopping a QProcess gracefully
a***@fastmail.fm
2014-08-07 06:19:49 UTC
Permalink
Hello,

I am trying to find the proper way to gracefully exit a QProcess. The
particular process that I am running does some work and then is in a
waiting state, exiting only when the user presses the Enter key. This
works fine when the executable is run from the command line.

The problem I am having is that every way I have tried to stop the
QProcess results in the error signal being emitted; in such cases,
errorString() always returns "Process crashed". I have tried:

(1) Calling QProcess::terminate()
(2) Calling QProcess::kill()
(3) Writing the return key to the process with QProcess::write("\r") and
QProcess::write("\\r)
(4) Sending a Qt::Key_Return using qApp->sendEvent() to the QProcess
object (though it's not clear to me that QKeyEvents sent to the QProcess
get automatically forwarded to the executable they have spawned).

In summary, how can I stop this QProcess properly without having the
error signal emitted?

Thanks!
Oswald Buddenhagen
2014-08-07 07:48:20 UTC
Permalink
Post by a***@fastmail.fm
(3) Writing the return key to the process with QProcess::write("\r") and
QProcess::write("\\r)
you need to use waitForBytesWritten() (or actually wait for the signal
to be delivered).
a***@fastmail.fm
2014-08-07 17:46:22 UTC
Permalink
Post by Oswald Buddenhagen
Post by a***@fastmail.fm
(3) Writing the return key to the process with QProcess::write("\r") and
QProcess::write("\\r)
you need to use waitForBytesWritten() (or actually wait for the signal
to be delivered).
Thanks! Forgot write was asynchronous. Using waitForBytesWritten() and
sending "\u000A" instead of "\r" or "\\r" did the trick.
Thiago Macieira
2014-08-07 14:28:37 UTC
Permalink
Post by a***@fastmail.fm
Hello,
I am trying to find the proper way to gracefully exit a QProcess. The
particular process that I am running does some work and then is in a
waiting state, exiting only when the user presses the Enter key. This
works fine when the executable is run from the command line.
This is not a question about Qt's own development. Please use the interest
Post by a***@fastmail.fm
The problem I am having is that every way I have tried to stop the
QProcess results in the error signal being emitted; in such cases,
(1) Calling QProcess::terminate()
(2) Calling QProcess::kill()
Those send a signal to the process, which means it will understand the process
as crashed. That's normal.

If the target process gracefully handles SIGTERM, then you can use it.
Post by a***@fastmail.fm
(3) Writing the return key to the process with QProcess::write("\r") and
QProcess::write("\\r)
As Ossi said, you need to verify that the byte was actually sent. Use
waitForBytesWritten or waitForFinished() to ensure it has finished.

Also, please make sure that the process is actually waiting for a Carriage
Return. I doubt that it is. The Enter key in a terminal does send a CR, but
the terminal translates it to a LF for the application. So you should try
sending "\n" instead.
Post by a***@fastmail.fm
(4) Sending a Qt::Key_Return using qApp->sendEvent() to the QProcess
object (though it's not clear to me that QKeyEvents sent to the QProcess
get automatically forwarded to the executable they have spawned).
That doesn't do anything. QProcess is not a graphical widget, so it doesn't
handle key events.

Option (5): closeWriteChannel(). Most shell processes terminate execution when
they get an EOF on stdin.
Post by a***@fastmail.fm
In summary, how can I stop this QProcess properly without having the
error signal emitted?
--
Thiago Macieira - thiago.macieira (AT) intel.com
Software Architect - Intel Open Source Technology Center
Loading...