zoukankan      html  css  js  c++  java
  • QProcess::startDetached(5.10有了一种新的方式)

    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 QProcesswithout 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 staticQProcess::startDetached() method.

    QProcess::startDetached("aplay tada.wav");

    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.

    qint64 pid;
    QProcess::startDetached("mpg123", {"Jeopardy_Theme.mp3"}, musicDirPath, &pid);
    printf("Performing a lengthy calculation...");
    calculateDeterminant(reallyBigMatrix);
    puts("done.");
    QProcess::startDetached("kill", {QString::number(pid)});

    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 stdinstdoutstderr 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:

    QProcess process;
    process.setProgram("mpg123");
    process.setArguments({"Jeopardy_Theme.mp3"});
    process.setWorkingDirectory(musicDirPath);
    process.setStandardOutputFile(QProcess::nullDevice());
    process.setStandardErrorFile(QProcess::nullDevice());
    qint64 pid;
    process.startDetached(&pid);
    ...

    The redirection to /dev/null makes sure that the user is not disturbed by visual output of the audio player.

    Only a certain sensible subset of functions is supported forstartDetached():

    • setArguments()
    • setCreateProcessArgumentsModifier()
    • setNativeArguments()
    • setProcessEnvironment()
    • setProgram()
    • setStandardErrorFile()
    • setStandardInputFile()
    • setStandardOutputFile()
    • setWorkingDirectory()

    All other properties of the QProcess object are ignored.

    If there are more properties to be supported in the future, they can be easily added in the implementation of the newstartDetached().

    http://blog.qt.io/blog/2017/08/25/a-new-qprocessstartdetached/

  • 相关阅读:
    《西北师范大学疫情防控信息系统》
    《西北师范大学学生疫情上报系统》项目报告
    201771010138-邹丰蔚 实验一 软件工程准备-<学习准备>
    201771030111-刘维 实验四 软件项目案例分析
    201771030111-刘维 实验三 结对项目—《西北师范大学疫情防控信息系统》项目报告
    MySql-5.7.29 zip版安装教程
    201771030111-刘维 实验二 个人项目—《西北师范大学学生疫情上报系统》项目报告
    201771030111-刘维 实验一 软件工程准备一<读《现代软件工程-构建之法》有问>
    201771030106-葛佳诚 实验四 软件项目案例分析
    201771030106-葛佳诚 实验三 结对项目—《西北师范大学疫情防控信息系统》项目报告
  • 原文地址:https://www.cnblogs.com/findumars/p/7433803.html
Copyright © 2011-2022 走看看