简体   繁体   English

Qt:QProcess结果与提示结果不匹配

[英]Qt: QProcess result doesn't match result at prompt

I'm trying to execute the following command from Qt: 我正在尝试从Qt执行以下命令:

explorer /select,C:\\Temp Folder\\temp.wav

This should show the file temp.wav as selected in an Explorer window. 这应该显示在资源管理器窗口中选择的文件temp.wav It works correctly when run from the command prompt. 从命令提示符运行时它可以正常工作。

However, when I try the following in Qt: 但是,当我在Qt中尝试以下内容时:

QProcess::startDetached(
            "explorer",
            QStringList("/select,C:\\Temp Folder\\temp.wav")
            );

it doesn't work -- it opens Explorer but puts me in the "My Documents" folder. 它不起作用 - 它打开资源管理器,但把我放在“我的文档”文件夹中。 If I rename the folder to one without a space (TempFolder), it works correctly. 如果我将文件夹重命名为没有空格的文件夹(TempFolder),它可以正常工作。

I've tried escaping the space in the folder name, placing quotes around the entire path, and many other combinations without success. 我已经尝试转义文件夹名称中的空格,在整个路径上放置引号,以及许多其他组合没有成功。 Many combinations work correctly in cmd but do not seem to work when called through QProcess::startDetached . 许多组合在cmd正常工作,但在通过QProcess::startDetached调用时似乎QProcess::startDetached

The most confusing part is that the code I'm trying to copy is from QtCreator source code where they use something similar to open up a file in the Explorer window. 最令人困惑的部分是我试图复制的代码来自QtCreator源代码,他们使用类似的东西在资源管理器窗口中打开一个文件。 Theirs successfully opens files with spaces in the path, but I just can't seem to recreate it! 他们成功地在路径中打开带有空格的文件,但我似乎无法重新创建它!

When you make such call: 当你打这样的电话时:

QProcess::startDetached("explorer",
                        QStringList("/select,C:\\Program Files\\7-Zip\\7z.exe"));

Qt transforms the argument string into the: Qt将参数字符串转换为:

explorer "/select,C:\\Program Files\\7-Zip\\7z.exe" 资源管理器“/ select,C:\\ Program Files \\ 7-Zip \\ 7z.exe”

which is not a valid option to open Explorer and select the given file. 这不是打开资源管理器并选择给定文件的有效选项。 This happens, because your single argument has space(s) and Qt escapes it with quotes. 发生这种情况,因为你的单个参数有空格,而Qt用引号转义它。 To fix this problem you need to make the following call: 要解决此问题,您需要进行以下调用:

QProcess::startDetached("explorer",
           (QStringList() << "/select," << "C:\\Program Files\\7-zip\\7z.exe"));

ie pass two arguments. 即传递两个参数。 This will produce the following string: 这将产生以下字符串:

explorer /select, "C:\\Program Files\\7-Zip\\7z.exe" explorer / select,“C:\\ Program Files \\ 7-Zip \\ 7z.exe”

which is valid and will do what is intended. 这是有效的,将做的是预期的。

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

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