Detaching a started process

后端 未结 2 514
暗喜
暗喜 2021-01-13 02:17

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

2条回答
  •  野趣味
    野趣味 (楼主)
    2021-01-13 02:32

    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();
    

提交回复
热议问题