简体   繁体   English

QProcess问题执行带有参数的exe

[英]QProcess issue in executing a exe with arguments

I have a issue in setting the QProcess to run executable with the arguments. 我在设置QProcess以使用参数运行可执行文件时遇到问题。 The Qt code for the same is as below, 相同的Qt代码如下,

QString program = "C:\Setup.exe";
QStringList arguments;
arguments << "-uninstall";
QProcess::startDetached(program, arguments);

The output of this snippet is to uninstall some program. 此代码段的输出是卸载某些程序。 But it is not happening. 但这没有发生。 Am i doing any mistake? 我有什么错误吗?

But if I go to cmd prompt and execute the same thing like., 但是,如果我进入cmd提示符并执行相同的操作,

c:/> "C:/Setup.exe" -uninstall This works perfectly. c:/>“ C:/Setup.exe” -uninstall完美运行。

There are at least two options to solve your issue. 至少有两个选项可以解决您的问题。

  • Use '/' for directory separators as per documentation: 根据文档使用“ /”作为目录分隔符:

    If you always use "/", Qt will translate your paths to conform to the underlying operating system. 如果您始终使用“ /”,则Qt将转换您的路径以符合底层操作系统。

  • Escape the backslash whenever working with file paths as string as per an example from the documentation: 每当从文档中将文件路径作为字符串使用时,转义反斜杠

     env.insert("TMPDIR", "C:\\\\MyApp\\\\temp"); // Add an environment variable env.insert("PATH", env.value("Path") + ";C:\\\\Bin"); 

Therefore, you should be writing something like this: 因此,您应该编写如下内容:

QString program = "C:/Setup.exe";
QStringList arguments;
arguments << "-uninstall";
QProcess::startDetached(program, arguments);

or this: 或这个:

QString program = "C:\\Setup.exe";
QStringList arguments;
arguments << "-uninstall";
QProcess::startDetached(program, arguments);

In general, when facing such issues, you could always print out the error string to get more information by using the following syntax: 通常,遇到此类问题时,可以始终使用以下语法打印出错误字符串以获取更多信息:

qDebug() << myProcess.errorString();

This, for sure, needs an instance, however. 当然,这需要一个实例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM