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/

最新文章

  1. PHP相关笔记
  2. 解决xcode6_beta没有代码提示的方法
  3. mysql优化技巧《转》
  4. php实现显示网站运行时间-秒转换年月日时分秒
  5. 【Asp.Net MVC-视频】
  6. Bolt 动画
  7. hdu 4267 树形DP
  8. 移动端 new CustomEvent('input') 兼容问题
  9. Arduino—运算符
  10. Android studio 编译出现的问题记录
  11. VS2017中VC++项目添加StringTable资源
  12. Python3实现Win10桌面背景自动切换
  13. c#比delphi先进的地方
  14. jquery的call()和apply()方法
  15. 菜鸟学Java(十四)——Java反射机制(一)
  16. 深入解析 ERP 计划的各个层次
  17. [转载]Vue 2.x 实战之后台管理系统开发(一)
  18. vue项目持久化存储数据的实现代码
  19. mysql5.7.22安装步骤
  20. Linux 学习第六天

热门文章

  1. Ubuntu12.04LTS SDK无法更新
  2. kernel-char设备的建立
  3. 1. java.util.concurrent - Java 并发工具包
  4. WP8.1开发:后台任务详解(求推荐)
  5. C++实现简单的内存块自己主动管理
  6. Atom编辑器折腾记_(13)JS代码智能提示补全(插件:atom-ternjs)
  7. 小米再迎两位重量级人才,原亦庄国投CEO王晓波入职产投部(产业嗅觉)
  8. sql server中查询结果集顺序问题
  9. hbase 2.0.2 分布式安装配置/jar包替换
  10. [Android]TextView实现分散对齐(两端对齐)