使用QProcessEnvironment更改cmd.exe的PATH环境变量 [英] Change PATH environment variable for cmd.exe with QProcessEnvironment

查看:2318
本文介绍了使用QProcessEnvironment更改cmd.exe的PATH环境变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从具有特定PATH集的Qt应用程序启动cmd.exe。我在QProcessEnvironment中插入路径并将该环境设置为QProcess。然后我startDetachedcmd。在命令提示符下,路径与来自调用应用程序的路径相同,而不是我刚才设置的路径。我错过了什么吗?我在Windows 8.1上使用Qt 5.2.0与mingw和Qt-creator 3.0.0。

I want to start cmd.exe from a Qt app with a specific PATH set. I insert "Path" in QProcessEnvironment and set that environment to QProcess. Then I startDetached "cmd". On the command prompt, the path is the same as the from the calling app, not what I just set. Did I miss something? I use Qt 5.2.0 with mingw and Qt-creator 3.0.0 on windows 8.1.s

QProcess process(this);
QProcessEnvironment env = QProcessEnvironment::systemEnvironment();
env.insert("Path", "MyPath");
process.setProcessEnvironment(env);
QStringList args;
args << "/D" << "/K" << "dir";
process.startDetached("cmd", args);


推荐答案

startDetached 方法是一个静态方法。所以你应用到进程对象的所有状态都被忽略,因为方法看不到它。如果你以 start()开始这个过程,新的进程会接收你的环境。

The startDetached method is a static method. So all the state that you applied to the process object is just ignored, because the method cannot see it. If you start the process with start() instead, the new process will pick up your environment.

process.start("cmd", args);

当然,你想要分离新进程,以便父进程可以终止而不强制新进程也终止。从我可以告诉, QProcess 类不提供你一个方法来实现这一点。您可以修改父进程的环境,以便新进程继承这些修改,但这听起来并不理想。

Of course, you want to the new process to be detached so that the parent can terminate without forcing the new process also to terminate. From what I can tell, the QProcess class does not offer you a way to achieve that easily. You could modify the environment of the parent process so that the new process inherits those modifications, but that does not sound at all desirable.

此问题提出了一个可能的解决方法: a href =http://stackoverflow.com/questions/17501642/detaching-a-started-process>分离已启动的进程。

This question presents a possible workaround: Detaching a started process.

这篇关于使用QProcessEnvironment更改cmd.exe的PATH环境变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆