Important: Please read the Qt Code of Conduct -

Finishing (not killing) a QProcess

  • Hi,

    I'm running a QProcess, and I need it to finish at some point. Right now I'm using

    but that kills the process. I need it to shut it down normally, because the process sends an OSC message on closing, and if I kill it it doesn't.

    Is there a method to do this, or should I code it myself? I checked the documentation but could not figure it out.


  • Moderators

    is "QProcess::terminate()": what you are looking for?

  • Sorry, I should've mentioned that I tried it but it doesn't work.

  • Moderators

    whats the behavior when calling QProcess::terminate()?

  • The process quits just like with QProcess::close()

  • You might want to look at QSharedMemory or QUdpSocket to command the process to cleanly shutdown, then call waitForFinished() on the QProcess (if you have the ability to edit the source of the child process to implement the shared memory or socket watch).

    You also can use IPC messaging built right into QProcess to send messages to the application using std out.

  • I can edit the source of the child process because I coded it, but if there's an easier way, I'd rather do that.

    Sending an exit command to the child process seems to be the way. Apparently

    does just that, but it ain't working. So I checked the documentation and found that maybe I could use something similar to this code
    @QProcess process1;
    QProcess process2;



    Although I get the idea, I don't know how to implement it in my case... Since I have just one child process, should I pipe the command from the Qt app to the child process? Can't I just send a command to the child process?

  • Moderators

    so how does your child-process react on the close event? Did you implement any special behavior so the app quits immediately?

  • bq. how does your child-process react on the close event?

    I don't quite understand what you mean by that. It just closes, like when killing it.

  • Moderators

    i meant did you reimplement the close event handler in your child-process application, so the event loop doesn't exits "cleanly"?

  • On (properly) closing, the child process sends an OSC message (implemented in the destructor of the child process).

    If I hit CMD-Q (or ESC) when the child-process window is focused, the message is sent, but if I close it with
    @myProcess->terminate() // or myProcess->close()@

    the message is not sent. Therefore, I assume that the process is abruptly killed. Is that what you where asking?

  • Moderators

    ok got you now.... somehow i thought you are working on a Windows system. On windows you receive a CLOSE event. On Unix-like system you receive SIGTERM signal which closes the application when they are not handled.

    So you will have to catch the SIGTERM signal and call qApp->closeAllWindows() for example.

    Todo so you can follow this "example": (code should be the same for Qt4).

  • I should've said that I was on a mac, sorry.

    I see what you mean. But this is platform-dependent, right? I'd have to catch the platform on which the app is running and then handle it appropriately.

    Anyhow, I think we're gonna go another way. We'll code the child process to terminate on receiving a "terminate" OSC message, that will be sent by the Qt app.

    I don't know if it's of your interest or it might help to understand the problem better, but maybe a short explanation of our project would be useful: we have a server app (the child process) and a client GUI (the Qt app I'm working on) that communicate via OSC messages. The idea is to be able to develop other clients with other languages in the future, hence the OSC. The GUI must be able to boot and halt the server and do some stuff after it has started or stopped.

    Thanks a lot for you help, anyway. It threw some light on the problem and helped me understand many things.


  • Moderators

    [quote author="ningu" date="1379498280"]I should've said that I was on a mac, sorry.[/quote]
    i could have asked so nvm.

    [quote author="ningu" date="1379498280"]
    I see what you mean. But this is platform-dependent, right? I'd have to catch the platform on which the app is running and then handle it appropriately.[/quote]
    yes it is. But you can easily handle this by using the "platform macro":

    If you follow my example it could be as simple as that:
    int main ()
    #if defined(Q_OS_UNIX)
    MyDaemon myDaemon;
    QApplication app;
    return app->exec();

  • Interesting. I may consider using it.

    Thanks again. You've been very helpful!

  • Moderators

    i've updated my post how you could use the macro with the example.

Log in to reply