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
stdin,stdout,stderrto files - and set native arguments and the
CreateProcessargument 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有了一种新的方式)的更多相关文章
- java 22 - 10 多线程之两种代码实现方式的比较与区别
- VMware的三种网络连接方式区别
关于VMware的三种网络连接方式,NAT,Bridged,Host-Only ,在刚接触的时候通常会遇到主机Ping不通虚拟机而虚拟机能Ping得通主机:主机与虚拟机互不相通等等网络问题.本文就这三 ...
- ASP.NET MVC下的四种验证编程方式[续篇]
在<ASP.NET MVC下的四种验证编程方式>一文中我们介绍了ASP.NET MVC支持的四种服务端验证的编程方式("手工验证"."标注Validation ...
- ASP.NET MVC下的四种验证编程方式
ASP.NET MVC采用Model绑定为目标Action生成了相应的参数列表,但是在真正执行目标Action方法之前,还需要对绑定的参数实施验证以确保其有效性,我们将针对参数的验证成为Model绑定 ...
- Windows 10 版本 1507 中的新 AppLocker 功能
要查看 Windows 10 版本信息,使用[运行]> dxdiag 回车 下表包含 Windows 10 的初始版本(版本 1507)中包括的一些新的和更新的功能以及对版本 1511 的 W ...
- ArrayList和LinkedList的几种循环遍历方式及性能对比分析(转)
主要介绍ArrayList和LinkedList这两种list的五种循环遍历方式,各种方式的性能测试对比,根据ArrayList和LinkedList的源码实现分析性能结果,总结结论. 通过本文你可以 ...
- ArrayList和LinkedList的几种循环遍历方式及性能对比分析
最新最准确内容建议直接访问原文:ArrayList和LinkedList的几种循环遍历方式及性能对比分析 主要介绍ArrayList和LinkedList这两种list的五种循环遍历方式,各种方式的性 ...
- JS几种数组遍历方式以及性能分析对比
前言 这一篇与上一篇 JS几种变量交换方式以及性能分析对比 属于同一个系列,本文继续分析JS中几种常用的数组遍历方式以及各自的性能对比 起由 在上一次分析了JS几种常用变量交换方式以及各自性能后,觉得 ...
- 二十八、带给我们一种新的编码思路——EFW框架CS系统开发中的MVC模式探讨
回<[开源]EFW框架系列文章索引> EFW框架源代码下载V1.3:http://pan.baidu.com/s/1c0dADO0 EFW框架实例源代码下载:http://p ...
随机推荐
- 【u117】队列安排
Time Limit: 1 second Memory Limit: 128 MB [问题描述] 一个学校里老师要将班上N个同学排成一列,同学被编号为1-N,他采取如下的方法: 1. 先将1号同学安排 ...
- IOS开发核心动画六:动画组
#import "ViewController.h" @interface ViewController () @property (weak, nonatomic) IBOutl ...
- Android Widget和悬浮窗 原理
1.简单介绍 Android widget是桌面插件,在android系统应用开发层面有特殊用途. AppWidget是把一个进程的控件嵌入到别外一个进程的窗口里的一种方法.悬浮窗的效果与Widget ...
- [Angular Unit Testing] Shallow Pipe Testing
import { TestBed, ComponentFixture } from '@angular/core/testing'; import { BrowserDynamicTestingMod ...
- .NET-架构优化实战-梳理篇
原文:.NET-架构优化实战-梳理篇 前言 程序员输出是他敲写的代码,那么输入就是他思考好的设计.因此不做设计是不存在,设计只分优秀的设计和糟糕的设计.为了避免过度设计浪费成本,需要针对现有业务与问题 ...
- 【59.49%】【codeforces 554B】Ohana Cleans Up
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- phpStudy的localhost不能访问怎么解决(相关性)
phpStudy的localhost不能访问怎么解决(相关性) 一.总结 1.注释掉httpd.conf文件中的#ServerName localhost:80 这句话. 2.既然是localho ...
- innodb_data_file_path参数误修改解决方法
如果innodb_data_file_path参数被修改乱了,不知道原来值的大小,这样启动是会报错的.知道原来大小方法: 方法一:操作系统下ls -l看到原来大小,直接用ls -l显示的大小,复制即可 ...
- Java 之 assert (断言)
我们知道C/C++语言中有断言的功能(assert).在Java SE 1.4版本以后也增加了断言的特性. 断言是为了方便调试程序,并不是发布程序的组成部分.理解这一点是很关键的. 默认情况下,JVM ...
- DOM常用的四大对象是什么?
DOM常用的四大对象是什么? 一.总结 一句话总结: 1.关注结构,关注主干 2.从主干处着手的话,可以发现dom就是四个东西,document(文档),element,attribute,event ...