[Development] Stopping a QProcess gracefully
Thiago Macieira
thiago.macieira at intel.com
Thu Aug 7 16:28:37 CEST 2014
On Wednesday 06 August 2014 23:19:49 achartier at fastmail.fm wrote:
> 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
mailing list next time. (and drop dev@ when replying to this email)
> 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()
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.
> (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.
> (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.
> 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
More information about the Development
mailing list