为什么我不能用这个功能在Qt框架中打开另一个进程?
Why can't I open another process in Qt framework with this function?
在 Qt 框架中,我们应该能够使用 QProcess 打开另一个 .exe。当我单击按钮并调用回调时,以下内容不起作用:
void MainWindow::on_pushButton_clicked()
{
QProcess *process = new QProcess(this);
QString wordPath = "C:/Program Files/Internet Explorer/iexplore.exe";
process->start(wordPath);
}
但是,如果我将 process->start(wordPath) 更改为:
process->start(wordPath, QStringList());
这是同一函数的重载,它可以工作。第二个参数应该是传递给您要启动的新进程的参数。我可以使单参数版本工作的唯一方法似乎是我的 PATH 变量中是否有某些内容,因为 "explorer.exe" 和 "msconfig" 都有效。这背后的故事是什么,只与第二个 QStringList() 一起工作,它只是一个空列表?
在另一个SO问题中我看到一个用户专门添加了一个空字符串,像这样:
QString wordPath = "C:\Program Files\Microsoft Office\Office12\WINWORD.EXE"
process->start(wordPath, QStringList() << "");
我很想知道这背后的原因是什么。
您的路径 "C:/Program Files/Internet Explorer/iexplore.exe" 包含空格,因此当您使用第一个版本时,程序被解释为 "C:/Program",参数为 "Files/Internet" 和 "Explorer/iexplore.exe"。
第二个版本将所有第一个参数视为程序,将 QStringList 视为参数。
来自 Qt 文档:
必须引用包含空格的参数才能正确提供给新进程。
试试看
QString wordPath = "\"C:/Program Files/Internet Explorer/iexplore.exe\"";
在 Qt 框架中,我们应该能够使用 QProcess 打开另一个 .exe。当我单击按钮并调用回调时,以下内容不起作用:
void MainWindow::on_pushButton_clicked()
{
QProcess *process = new QProcess(this);
QString wordPath = "C:/Program Files/Internet Explorer/iexplore.exe";
process->start(wordPath);
}
但是,如果我将 process->start(wordPath) 更改为:
process->start(wordPath, QStringList());
这是同一函数的重载,它可以工作。第二个参数应该是传递给您要启动的新进程的参数。我可以使单参数版本工作的唯一方法似乎是我的 PATH 变量中是否有某些内容,因为 "explorer.exe" 和 "msconfig" 都有效。这背后的故事是什么,只与第二个 QStringList() 一起工作,它只是一个空列表?
在另一个SO问题中我看到一个用户专门添加了一个空字符串,像这样:
QString wordPath = "C:\Program Files\Microsoft Office\Office12\WINWORD.EXE"
process->start(wordPath, QStringList() << "");
我很想知道这背后的原因是什么。
您的路径 "C:/Program Files/Internet Explorer/iexplore.exe" 包含空格,因此当您使用第一个版本时,程序被解释为 "C:/Program",参数为 "Files/Internet" 和 "Explorer/iexplore.exe"。 第二个版本将所有第一个参数视为程序,将 QStringList 视为参数。
来自 Qt 文档:
必须引用包含空格的参数才能正确提供给新进程。
试试看
QString wordPath = "\"C:/Program Files/Internet Explorer/iexplore.exe\"";