英文:
Weird issue with Windows: Qt 5.15.2
问题 {#heading}
Platform Qt 5.15.2 Windows.
我有一个使用以下代码的函数:
QString commstr = "c:/temp/cpath.bat";
QProcess::startDetached("cmd.exe", QStringList() << "/c" << "start" << commstr);
批处理文件:
echo off
set PATH=%PATH%; 更多路径在此添加 ...
它会打开一个控制台并运行QString commstr
中包含的.bat
文件。批处理文件会对路径进行临时更改,然后完成,但会保留控制台以供用户进一步交互。一切都很正常,正如预期的那样。
这里有一个奇怪的问题:如果用户名中有嵌入的空格,例如 "joe user",则批处理文件将无法运行。控制台会打开,但commstr
永远不会被执行。我无法理解为什么用户名在这里会有影响。
我无法想象用户名在此起到了什么作用。代码中没有引用用户的主目录(例如 "C:\Users\joe user"),程序也没有安装在那里。程序在其他带有嵌入空格的目录(例如 "C:\Program Files")上无问题运行。 英文:
Platform Qt 5.15.2 Windows.
I have a function that uses this code:
QString commstr = "c:/temp/cpath.bat";
`QProcess::startDetached("cmd.exe", QStringList() << "/c" << "start" << commstr);
`
bat file:
echo off
set PATH=%PATH%; more paths added here ...
It opens a console and runs the .bat file contained in the QString commstr
. The bat file makes a temporary change to the path then finishes, leaving the console open for further user interaction. Works great, just as intended.
Here's the weird problem: If the userid has embedded spaces i.e. "joe user" the .bat file is not run. The console opens, but commstr
is never executed. I can't for the life of me figure out why the userid matters here.
I can't imagine what part the userid plays in this. The user's home directory (e.g. "C:\Users\joe user") isn't referenced in the code, nor is the program installed there. The program works w/o issues other embedded spaces directories anyway (e.g "C:\Program Files").
答案1 {#1}
得分: 0
QProcess::startDetached("cmd.exe", QStringList() << "/c" << "start" << "" << "/path/to/file.bat");
在start
命令之后添加一个空的标题字符串作为第一个参数可以解决我的问题。
语法
START "标题" [/D 路径] [选项] "命令" [参数]
关键词:
标题 CMD窗口标题栏上的文本(必需)
路径 起始目录。
命令 要运行的命令、批处理文件或可执行程序。
参数 传递给命令的参数。
英文:
QProcess::startDetached("cmd.exe", QStringList() << "/c" << "start" << "" << "/path/to/file.bat");
Adding a empty title string as the first argument following start fixed my issue.
Syntax
START "title" [/D path] [options] "command" [parameters]
Key:
title Text for the CMD window title bar (required.)
path Starting directory.
command The command, batch file or executable program to run.
parameters The parameters passed to the command.\`\`\`
\</details\>