Search code examples
c++qtkillqthreadqprocess

QThread::terminate vs kill


I have a BASH script run (QProcess blocking) in a QThread (in C++). This BASH script tars lots of files and can run for 1/2 hour.

In case the user wants to shutdown my program I need to kill my BASH script. But how? QThread::Quit will wait for the BASH program to terminate before processing signals, QThread::Terminate documentation says it MAY kill a thread immediately.

I want the equivalent of 'kill -9 myscript'. Is there a proper Qt way to do this?


Solution

    1. Do not use additional threads. It's never necessary.
    2. Never use any waitForXxx methods.
    3. Use QProcess::kill to kill the process.
    4. Use QProcess's signals to get notified when the process changes state, e.g. is finished.