我尝试使用 QProcess
(在Windows上)从我的程序(FaceModifier.exe)中启动blender.exe . 该命令遵循以下结构:
'path-to-blender' - background'path-to-blend-file'-python'path-to-python-script' - 'additional-arg-for-python-script'
一个完整的例子(如果我将其键入cmd.exe,则可以正常工作)
“C:\ Program Files \ Blender Foundation \ Blender \ blender.exe” - background“C:\ Program Files(x86)\ FaceModifier \ Resources \ GenericHeadMesh.blend”--python“C:\ Program Files(x86) \ FaceModifier \ python \ local.py“ - ”C:\ Users \ Gunnar \ Documents \ FaceModifier \ Output \“
现在,在我的程序中,我逃避路径并用引号包围它们所以我有这样的东西
std::string blenderPath := "\"C:\\Program Files\\Blender Foundation\\Blender\\blender.exe\""
对于QProcess,我将所有参数都输入到带有前导 /c
的QStringList中,因此它被视为单个命令并将其传递给 cmd.exe
.
我的问题是我不能让它被执行 . 如果我手动输入命令(我传递给QProcess,而不是上面的命令)到cmd .
我启动进程的函数如下所示:
void PythonConnector::createSubprocess(const QStringList &args)
{
QProcess *process = new Process();
process->start("cmd.exe", args);
process->waitForFinished(-1);
QString result(process->readAll());
qDebug() << "result: " << result; // this gives just an empty string
process->close();
}
我称之为:
// for the documents path
CoInitialize(NULL);
TCHAR *myDocuments = 0;
SHGetKnownFolderPath(FOLDERID_Documents, 0, NULL, &myDocuments);
CString temp(myDocuments);
CT2CA tempConv(temp);
std::string outputFolderPath(tempConv);
outputFolderPath += "\\FaceModifier\\Output\\";
outputFolderPath = pH.ExcapeString(outputFolderPath);
CoTaskMemFree(myDocuments);
blenderPath = pH.EscapeString(blenderPath);
std::string meshGlobalPath = "\"" + pH.GetResourcesPath() + "GenericHeadMesh.blend" + "\"";
std::string pythonGlobalPath = "\"" + pH.GetPythonPath() + "global.py" + "\"";
QStringList args;
args << "/c" << QString::fromStdString(blenderPath) << "--background" << QString::fromStdString(meshGlobalPath) << "--python" << QString::fromStdString(pythonGlobalPath) << "--" << QString::fromStdString("\"" + outputFolderPath "\"");
pC.createSubprocess(args);
blenderPath
传递给此函数并从另一个函数的注册表中读取 .
这些是我的其他辅助函数:
std::string PathHandler::GetResourcesPath()
{
if(resourcesPath.empty())
resourcesPath = GetFullPath("Resources\\");
return resourcesPath;
}
std::string PathHandler::GetPythonPath()
{
if(pythonPath.empty())
pythonPath = GetFullPath("python\\");
return pythonPath;
}
std::string PathHandler::GetFullPath(const std::string &relPath)
{
char full[_MAX_PATH];
_fullpath(full, relPath.c_str(), _MAX_PATH);
return EscapeString(std::string(full));
}
std::string PathHandler::EscapeString(const std::string &input)
{
std::regex toReplace("\\\\");
std::string output(input.begin(), input.end());
output = std::regex_replace(output, toReplace, "\\\\");
return output;
}
使用qDebug调试args列表会导致这些输出(这些实际上来自Visual Studio 2013中的调试,因此是不同的路径):
"/c"
""C:\\Program Files\\Blender Foundation\\Blender\\blender.exe""
"--background"
""C:\\Users\\Gunnar\\documents\\visual studio 2013\\Projects\\FaceModifier\\x64\\Release\\Resources\\GenericHeadMesh.blend""
"--python"
""C:\\Users\\Gunnar\\documents\\visual studio 2013\\Projects\\FaceModifier\\x64\\Release\\python\\global.py""
"--"
""C:\\Users\\Gunnar\\Documents\\FaceModifier\\Output\\""
而 createSubprocess
的 result
调试只给出 ""
.
如果我手动将此命令输入cmd,如下所示:
cmd / c“C:\ Program Files \ Blender Foundation \ Blender \ blender.exe”--background“C:\ Users \ Gunnar \ documents \ visual studio 2013 \ Projects \ FaceModifier \ x64 \ Release \ Resources \ GenericHeadMesh.blend “--python”C:\ Users \ Gunnar \ documents \ visual studio 2013 \ Projects \ FaceModifier \ x64 \ Release \ python \ global.py“ - ”C:\ Users \ Gunnar \ Documents \ FaceModifier \ Output \“
我得到 The command "C:\\Program" is either missspelled or couldn't be found.
相同的各种不同的报价,有和没有这样的逃避
cmd“/ c \”C:\ Program Files \ Blender Foundation \ Blender \ blender.exe \“ - background \”C:\ Users \ Gunnar \ documents \ visual studio 2013 \ Projects \ FaceModifier \ x64 \ Release \ Resources \ GenericHeadMesh.blend \“ - python \”C:\ Users \ Gunnar \ documents \ visual studio 2013 \ Projects \ FaceModifier \ x64 \ Release \ python \ global.py \“ - \”C:\ Users \ Gunnar \文档\ FaceModifier \输出\ “”
只是打字
cmd / c“C:\ Program Files \ Blender Foundation \ Blender \ blender.exe”
工作正常,但这显然不是我需要的 .
我不知道如何构建它以便它可以工作 . 有人能告诉我我的错误是什么吗?
UPDATE:
好吧,从理论上说它现在有效(感谢艾哈迈德),但它确实在路径上黯然失色 . 如果我在 ...\Documents\FaceModifier
中有.blend和.py,那么如果它们在 C:\Program Files (x86)\FaceModifier\
(安装了我的程序),它就不会在那里复制那些文件 .
1 回答
您不需要引用或双引号文件路径,QProccess已经处理过,将此部分代码更改为: