我需要的东西很容易,其保持在不工作的一些帮助:Qt的C++ - 构建QStringList中使用双引号
我想conctruct这个QStringList中:
-c "import imp; foo = imp.load_source('myTest', '/home/John/fw/demos/myTest.py'); foo.myTestFunction();"
注意报价!但我已经尝试过几十亿种东西,例如:
QStringList params;
params << "-c \"import imp; foo = imp.load_source('myTest', '/home/John/fw/demos/myTest.py'); foo.myTestFunction() \" ";
但是,这似乎并不奏效!
会发生什么:
我想从我的Qt图形界面运行Python脚本。我可以运行的时候我使用的终端如下脚本:
$ python2 -c "import imp; foo = imp.load_source('myTest', '/home/John/fw/demos/myTest.py'); foo.myTestFunction();"
output: this is the python script
现在我想在我的Qt C++代码复制此如下:
{
QProcess p;
QStringList params;
params << "-c \"import imp; foo = imp.load_source('myTest', '/home/John/fw/demos/myTest.py'); foo.myTestFunction() \" ";
qDebug()<<params;
p.start("python2 ", params);
p.waitForFinished(-1);
QString p_stdout = p.readAll();
qDebug()<<p_stdout;
qDebug()<<"ran py script";
}
但它只输出:
"-c \"import imp; foo = imp.load_source('myTest', '/home/John/fw/demos/myTest.py'); foo.myTestFunction() \" "
""
ran py scrip
这是脚本:
#!/usr/bin/env python2
import os
import numpy as np
def myTestFunction():
print('output: this is the python script')
return 55
if __name__=='__main__':
a = myTestFunction()
print('returned value: '+ str(a))
我预计至少看到“55”而不是像“”的空字符串。有人可以帮我吗?
QStringList不是解析器。如果你需要一个字符串列表,你需要提供它的元素作为单独的字符串。 –
@ n.m。你可以请*显示*你的意思?我已经尝试了很多东西,而且chanxes非常高,已经尝试了我所建议的。 – LandonZeKepitelOfGreytBritn
尝试'params <<“-c”<<“import imp; foo ... etc”'(注意你可能**不需要在第二个参数周围需要额外的引号)。 –