From Qt 5.10 on, there is a new way how to start detached processes with QProcess.
Of course you know this, but let me quickly repeat what a detached process is. If you start a program using QProcess without detaching, then the destructor of QProcess will terminate the process. In contrast, a detached process keeps running unaffected when the calling process exits. On Unix, a detached process will run in its own session and act like a daemon.
Traditionally, we start detached processes with the static QProcess::startDetached() method.
There is a second overload that supports passing a separate argument list and the working directory. It also lets us retrieve the PID of the started process.
This little example crunches numbers for a while and plays the Jeopardy Theme to entertain the user. When the calculation result is ready, it kills the music player using the obtained PID.
When running this example on Linux, you will notice a little annoyance: we have no way of suppressing the output of the detached process. (Okay, this particular tool has the "--quiet" option, but let's ignore that for the example's sake.)
Qt Users have requested for a long long time the ability to
set the process environment,
redirect stdin, stdout, stderr to files
and set native arguments and the CreateProcess argument modifier on Windows
for detached processes.
All this is now possible in Qt 5.10! But instead of adding a monstrous overload for the static startDetached() method, we added a non-static QProcess::startDetached(qint64 *pid). This new member function reads the properties of your QProcess object and starts the to-be-detached process accordingly. It can be used as follows: