Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I've started a process using QProcess::start() and I need to detach it afterwards. How can I do it? I haven't found relevant info in the Qt docs.

I'm aware of QProcess::startDetached(), but due to other code in the program, I can't use it (I need to separate the starting and the detaching of the process).

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
468 views
Welcome To Ask or Share your Answers For Others

1 Answer

If you take a look into the implementation of QProcess::~QProcess(), you will know how QProcess terminates the process with its destruction. Also, note that QProcess::setProcessState() is protected, which means you could implement a QDetachableProcess inherited from QProcess with a method detach() to call setProcessState(QProcess::NotRunning); as a workaround.

For example:

class QDetachableProcess : public QProcess
{
public:
    QDetachableProcess(QObject *parent = 0) : QProcess(parent){}
    void detach()
    {
        this->waitForStarted();
        setProcessState(QProcess::NotRunning);
    }
};

Then you could do things like this:

QDetachableProcess process;
process.setEnvironment(QStringList() << "SOME_ENV=Value");

process.start();

process.detach();

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...