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/

时间: 2024-08-12 12:21:00

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

用Docker在一台笔记本电脑上搭建一个具有10个节点7种角色的Hadoop集群(下)-搭建Hadoop集群

上篇:用Docker在一台笔记本电脑上搭建一个具有10个节点7种角色的Hadoop集群(上)-快速上手Docker 上篇介绍了快速上手Docker部分,下面接着介绍搭建Hadoop集群部分. 六.搭建Hadoop伪分布模式 我们先用前面创建的这个容器来搭建Hadoop伪分布模式做测试,测试成功后再搭建完全分布式集群. 1.SSH这个centos容器可以看做是一个非常精简的系统,很多功能没有,需要自己安装.Hadoop需要SSH,但容器没有自带,需要我们安装.①安装SSH # yum -y ins

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

在<ASP.NET MVC下的四种验证编程方式>一文中我们介绍了ASP.NET MVC支持的四种服务端验证的编程方式("手工验证"."标注ValidationAttribute特性"."让数据类型实现IValidatableObject或者IDataErrorInfo"),那么在ASP.NET MVC框架内部是如何提供针对这四种不同编程方式的支持的呢?接下来我们就来聊聊这背后的故事. 一.ModelValidator与ModelVal

LVS:三种负载均衡方式比较+另三种负载均衡方式

转:http://blog.csdn.net/u013256816/article/details/50705578 什么是LVS? ??首先简单介绍一下LVS (Linux Virtual Server)到底是什么东西,其实它是一种集群(Cluster)技术,采用IP负载均衡技术和基于内容请求分发技术.调度器具有很好的吞吐率,将请求均衡地转移到不同的服务器上执行,且调度器自动屏蔽掉服务器的故障,从而将一组服务器构成一个高性能的.高可用的虚拟服务器.整个服务器集群的结构对客户是透明的,而且无需修

android五种数据存储方式

在Android中提供了如下五种数据存储方式: SharedPreferences 文件存储 SQLite数据库方式 内容提供器(ContentProvider) 网络存储 下面具体介绍一下这五种数据存储方式的用法. 1. SharedPreferences SharedPreferences是android平台上的一个轻量级存储类,主要保存一些常用的配置信息.可以保存long.int.String类型数据. SharedPreferences以键值对方式读取和存入,最终会以XML方式保存数据,

Android4种网络连接方式HttpClient、HttpURLConnection、OKHttp和Volley优缺点和性能对比

比较的指标: 1.cpu 2.流量 3.电量 4.内存占用 5.联网时间 功能点: 1.重试机制 2.提供的扩展功能 3.易用性 4.是否https 5.是否支持reflect api,OkHttp有配套方法 6.缓存.重试 7.cookie支持session  id会话支持 8.弱网性能和稳定性 9.超时时间,几种超时时间   连接超时,响应超时 10.适配各种机型.4.4和之前版本  2.3  4.1 5.0 4种网络连接方式提供的功能对比表格: 缓存 重试 Https/Http 稳定性 C

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

回<[开源]EFW框架系列文章索引>        EFW框架源代码下载V1.3:http://pan.baidu.com/s/1c0dADO0 EFW框架实例源代码下载:http://pan.baidu.com/s/1eQCc69G       前言:记得最初写出Winform版MVC的代码是在公司的一个产品中,产品有几个界面功能比较多,一个界面窗体的代码尽然有1万多行代码,让我们在维护这几个界面的时候非常的痛苦,你可能想可以把这个大的界面拆分成几个小的界面在集成在一起不就好了,但实际上这样

几种VPN组网方式介绍

几种VPN组网方式介绍 VPN是(Virtual Private Network ,虚拟专用网络)的简称,最初是指通过公用网络(例如Internet)将多个不同地区的局域网,组建成广域网的一项技术.为什么要用VPN组网呢?我们简要介绍一下. 在Internet没有普及的时候,跨地区的单位如果要组建网络(广域网)需要从电信租用点对点或点对多点的"专线",例如某个单位总部在北京,在天津.广州.上海有分公司,如果这4个点要组成一个"大局域网",就需要租用3条专线,分别是从

hive--udf函数(开发-4种加载方式)

h2 { color: #fff; background-color: #7CCD7C; padding: 3px; margin: 10px 0px } h3 { color: #fff; background-color: #008eb7; padding: 3px; margin: 10px 0px } UDF函数开发 标准函数(UDF):以一行数据中的一列或者多列数据作为参数然后返回解雇欧式一个值的函数,同样也可以返回一个复杂的对象,例如array,map,struct. 聚合函数(UD

Apache中3种MPM工作方式的区别及配置详解

MPM是Multi-Processing Modules,表示Apache中的多路处理模块,目前在Linux上的Apache2.2/2.4中包括三种模式:prefork.worker和event模式.apache或httpd的命令参数"-l"即可列出当前apache中已经编译了的模块,由于MPM只能在编译时指定其中一种,所以列出的静态模块中MPM只有1个,如下所示: # httpd -l Compiled in modules: core.c prefork.c    #说明使用的是p