[英]QProcess not executing a python script
I'm trying to execute a process in Qt (in Linux) that executes a python script and I haven't been able to make it work.我正在尝试在 Qt(在 Linux 中)中执行一个执行 python 脚本的进程,但我无法使其工作。 This is the code:
这是代码:
QProcess process;
QString scriptFile = "../../scriptPath/script.py";
QString pyCommand = "\"python " + scriptFile +
" -f " + parameter1 +
" -t parameter2" +
" -v parameter3" +
" -e " + parameter4 + "\"";
printf("PyCommand: %s\n", qStringToStdString(pyCommand).c_str());
process.start ("bash", QStringList () << "-c" << pyCommand);
Where parameter1
and parameter3
are QStrings that I get from file dialogs (both paths are correct) and parameter2
and parameter4
are hardcoded strings (just in case, these strings have characters "-" and "." in them).其中
parameter1
和parameter3
是我从文件对话框中获得的 QStrings(两个路径都是正确的), parameter2
和parameter4
是硬编码的字符串(以防万一,这些字符串中包含字符“-”和“.”)。 Function qStringToStdString
is a function I have created to transform a QString
to a std::string
and it works just fine (I already have used it).函数
qStringToStdString
是我创建的一个函数,用于将QString
转换为std::string
并且它工作得很好(我已经使用过它)。
PyCommand is printed in the console and looks fine. PyCommand 打印在控制台中,看起来不错。 In fact, if I open a terminal and try to execute
bash -c
followed by what I get in the printf instruction it works.事实上,如果我打开一个终端并尝试执行
bash -c
然后是我在 printf 指令中得到的内容,它就会起作用。 But it doesn't work when I execute the Qt application.但是当我执行 Qt 应用程序时它不起作用。
Can someone help me out here?有人可以帮我吗? Something I'm missing?
我错过了什么?
Your code is ignoring how the API was designed to work.您的代码忽略了 API 的工作原理。 You should use either of the alternatives presented below.
您应该使用下面介绍的任何一种替代方法。 Using the
python
interpreter explicitly depends on whether you use the shebang properly in your python script.明确使用
python
解释器取决于您是否在 python 脚本中正确使用了shebang。 I have just let it in for reference, but you can easily get rid of that.我只是让它作为参考,但你可以很容易地摆脱它。 It is not the main point of your issue in here.
这不是您问题的重点。
QStringList
based)QStringList
)QProcess process;
QString scriptFile = QCoreApplication::applicationDirPath() + "../../scriptPath/script.py";
QStringList pythonCommandArguments = QStringList() << scriptFile
<< "-f " << parameter1 << "-t" << parameter2 << "-v"
<< parameter3 << "-e" << parameter4;
printf("PyCommand: %s\n", qStringToStdString(pythonCommandArguments.join(' ')).c_str());
process.start ("python", pythonCommandArguments);
QString
based)QString
)QProcess process;
QString scriptFile = QCoreApplication::applicationDirPath() + "../../scriptPath/script.py";
QString pythonCommand = "python " + scriptFile +
" -f " + parameter1 +
" -t parameter2" +
" -v parameter3" +
" -e " + parameter4;
printf("PyCommand: %s\n", qStringToStdString(pythonCommand).c_str());
process.start (pythonCommand);
Here you can find the proper method signatures for both ways:在这里,您可以找到两种方式的正确方法签名:
void QProcess::start(const QString & program, const QStringList & arguments, OpenMode mode = ReadWrite)
http://doc.qt.io/qt-5/qprocess.html#start http://doc.qt.io/qt-5/qprocess.html#start
void QProcess::start(const QString & command, OpenMode mode = ReadWrite)
http://doc.qt.io/qt-5/qprocess.html#start-3 http://doc.qt.io/qt-5/qprocess.html#start-3
You need to use QStringList()
for command line parameter.您需要使用
QStringList()
作为命令行参数。
Your pyCommand
must be QStringList
.您的
pyCommand
必须是QStringList
。
Something like this:像这样的东西:
QStringList pyCommand;
pyCommand << "-f" << parameter1 <<
"-t" << parameter2 <<
"-v" << parameter3 <<
"-e" << parameter4;
process.start(scriptFile, pyCommand);
See the docs: http://doc.qt.io/qt-5/qprocess.html#start查看文档: http : //doc.qt.io/qt-5/qprocess.html#start
So at the end I got it working.所以最后我让它工作了。 As Laszlo commented out, the way it should be executed is starting a process with
python
and pass all the arguments as a QStringList
.正如 Laszlo 所评论的那样,它应该执行的方式是使用
python
启动一个进程并将所有参数作为QStringList
传递。 That's what I tried at the beginning and it wasn't working at all.这就是我一开始尝试的方法,但它根本不起作用。 So I looked for information on the internet and I saw that many people were executing a
bash
process and then they were passing a string with the python command instead of doing it as I was.所以我在互联网上寻找信息,我看到很多人正在执行一个
bash
进程,然后他们用 python 命令传递一个字符串,而不是像我那样做。 I tried to do it that way but it wasn't working either...我试图这样做,但它也不起作用......
The solution?解决办法? Well, I've done what I should have done before asking the question here.
好吧,在问这里问题之前,我已经做了我应该做的事情。 I connected the most important signals of the process (started, finished and error) with some simple methods that wrote output.
我将过程中最重要的信号(开始、完成和错误)与一些写入输出的简单方法联系起来。 At the end I found out that python was returning
2
(No such file or directory) as the exitCode
.最后我发现 python 返回
2
(没有这样的文件或目录)作为exitCode
。
It seems that the application wasn't taking the executable path as the root path and therefore the path I was referring to ( "../../scriptPath/script.py"
) didn't exist.应用程序似乎没有将可执行路径作为根路径,因此我所指的路径(
"../../scriptPath/script.py"
)不存在。 In order to get the proper binary path I use now QDir::currentPath() + "/scriptPath/script.py"
.为了获得正确的二进制路径,我现在使用
QDir::currentPath() + "/scriptPath/script.py"
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.