What is the difference between QProcess::start and QProcess::startDetached?

后端 未结 2 1604
半阙折子戏
半阙折子戏 2021-02-05 23:46

The Qt documentation gives this explanation:

  • QProcess::start:

    Starts the given program in a new process, if none is already

相关标签:
2条回答
  • 2021-02-06 00:20

    If you use start, termination of the caller process will cause the termination of the called process as well. If you use startDetached, after the caller is terminated, the child will continue to live. For example:

    QProcess * p = new QProcess();
    p->start("some-app");
    delete p;// <---some-app will be terminated
    
    QProcess * p = new QProcess();
    p->startDetached("some-app");
    delete p;// <---some-app will continue to live
    
    0 讨论(0)
  • 2021-02-06 00:32

    The start() function is a member function, while startDetached is a static class function.

    If you look at the documentation of QProcess, you'll see that there are functions to allow you to do things with the process such as: -

    • Receive the output or error streams from the running process (readAllStandardOutput / readAllStandardError)
    • Redirect the output to a file (setStandardOutputFile)
    • Use a file for standard input into the process (setStandardInputFile)
    • Communicate via channels
    • Get notified when the process has finished

    These are just some of the things that you can only do with an instance of a QProcess. If, however, you want a simple and quick way of starting a process without having to create an instance and you don't need the extra functionality, you can simply call QProcess::startDetached.

    Also, as the docs state for startDetached: -

    If the calling process exits, the detached process will continue to live.

    0 讨论(0)
提交回复
热议问题