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/

QProcess::startDetached(5.10有了一种新的方式)的更多相关文章

  1. java 22 - 10 多线程之两种代码实现方式的比较与区别

  2. VMware的三种网络连接方式区别

    关于VMware的三种网络连接方式,NAT,Bridged,Host-Only ,在刚接触的时候通常会遇到主机Ping不通虚拟机而虚拟机能Ping得通主机:主机与虚拟机互不相通等等网络问题.本文就这三 ...

  3. ASP.NET MVC下的四种验证编程方式[续篇]

    在<ASP.NET MVC下的四种验证编程方式>一文中我们介绍了ASP.NET MVC支持的四种服务端验证的编程方式("手工验证"."标注Validation ...

  4. ASP.NET MVC下的四种验证编程方式

    ASP.NET MVC采用Model绑定为目标Action生成了相应的参数列表,但是在真正执行目标Action方法之前,还需要对绑定的参数实施验证以确保其有效性,我们将针对参数的验证成为Model绑定 ...

  5. Windows 10 版本 1507 中的新 AppLocker 功能

    要查看 Windows 10 版本信息,使用[运行]> dxdiag  回车 下表包含 Windows 10 的初始版本(版本 1507)中包括的一些新的和更新的功能以及对版本 1511 的 W ...

  6. ArrayList和LinkedList的几种循环遍历方式及性能对比分析(转)

    主要介绍ArrayList和LinkedList这两种list的五种循环遍历方式,各种方式的性能测试对比,根据ArrayList和LinkedList的源码实现分析性能结果,总结结论. 通过本文你可以 ...

  7. ArrayList和LinkedList的几种循环遍历方式及性能对比分析

    最新最准确内容建议直接访问原文:ArrayList和LinkedList的几种循环遍历方式及性能对比分析 主要介绍ArrayList和LinkedList这两种list的五种循环遍历方式,各种方式的性 ...

  8. JS几种数组遍历方式以及性能分析对比

    前言 这一篇与上一篇 JS几种变量交换方式以及性能分析对比 属于同一个系列,本文继续分析JS中几种常用的数组遍历方式以及各自的性能对比 起由 在上一次分析了JS几种常用变量交换方式以及各自性能后,觉得 ...

  9. 二十八、带给我们一种新的编码思路——EFW框架CS系统开发中的MVC模式探讨

    回<[开源]EFW框架系列文章索引>        EFW框架源代码下载V1.3:http://pan.baidu.com/s/1c0dADO0 EFW框架实例源代码下载:http://p ...

随机推荐

  1. 【心情】Priority_queue容器的用法

    所给的代码最顶端是最小的元素 要改为最顶端是最大的则只需把 friend bool operator<(Node a, Node b) { return a.val > b.val; } ...

  2. Android JNI编程(七)——使用AndroidStudio编写第一个JNI程序

    版权声明:本文出自阿钟的博客,转载请注明出处:http://blog.csdn.net/a_zhon/. 目录(?)[+] 1.简单介绍一下NDK和JNI NDK:NDK是Native Develop ...

  3. Android5.0网络之ipv6

    移动设备的大量兴起势必进一步加强ip地址不足的危机. ipv6或许成为一种比較好的选择方案. ipv6地址的获取分为两种方式:无状态:有状态 无状态:通过接收路由公告(RA)来设置自己的ipv6地址 ...

  4. Hadoop源码分析(MapReduce概论)

    大家都熟悉文件系统,在对HDFS进行分析前,我们并没有花非常多的时间去介绍HDFS的背景.毕竟大家对文件系统的还是有一定的理解的,并且也有非常好的文档.在分析Hadoop的MapReduce部分前,我 ...

  5. Android接口安全 - RSA+AES混合加密方案

    转载请注明出处: http://blog.csdn.net/aa464971/article/details/51034462 本文以Androidclient加密提交数据到Java服务端后进行解密为 ...

  6. php标准库spl栈SplStack如何使用?

    php标准库spl栈SplStack如何使用? 一.总结 php标准库spl栈SplStack介绍.(SplStack类)(各种方法都支持) 1.SplStack类:$stack = new SplS ...

  7. 浏览器对象模型(BOM)是什么?(体系结构+知识详解)(图片:结构)

    浏览器对象模型(BOM)是什么?(体系结构+知识详解)(图片:结构) 一.总结 1.BOM操作所有和浏览器相关的东西:网页文档dom,历史记录,浏览器屏幕,浏览器信息,文档的地址url,页面的框架集. ...

  8. PHP移动互联网开发笔记(8)——MySQL数据库基础回顾[2]

    一.数据表 为了确保数据的完整性和一致性,在创建表时指定字段名称,字段类型和字段属性外,还需要使用约束(constraint),索引(index),主键(primary key)和外键(foregin ...

  9. 【a703】求逆序对(线段树的解法)

    Time Limit: 10 second Memory Limit: 2 MB 问题描述 给定一个序列a1,a2...an.如果存在i小于j 并且ai大于aj,那么我们称之为逆序对,求给定序列中逆序 ...

  10. PHP正则表达式怎么匹配多行

    PHP正则表达式怎么匹配多行 一.总结 1.可以用匹配模式修饰符s来使点匹配所有字符,包括换行符. 二.PHP匹配多行的正则表达式分析 代码如下: <table> <tr>&l ...