2017-08-10 15 views
1

我需要的东西很容易,其保持在不工作的一些帮助: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”而不是像“”的空字符串。有人可以帮我吗?

+0

QStringList不是解析器。如果你需要一个字符串列表,你需要提供它的元素作为单独的字符串。 –

+0

@ n.m。你可以请*显示*你的意思?我已经尝试了很多东西,而且chanxes非常高,已经尝试了我所建议的。 – LandonZeKepitelOfGreytBritn

+0

尝试'params <<“-c”<<“import imp; foo ... etc”'(注意你可能**不需要在第二个参数周围需要额外的引号)。 –

回答

1

当程序使用单独的参数运行时,它返回一个空字符串,我仍然不明白原因。但是,如果我们在一个命令串联一切它能够正确执行应用程序:

#include <QCoreApplication> 
#include <QProcess> 
#include <QDebug> 

int main(int argc, char *argv[]) 
{ 
    QCoreApplication a(argc, argv); 

    QProcess p; 
    QString cmd("/usr/bin/python2 -c \"import imp; foo = imp.load_source(\'myTest\', \'/home/qhipa/myTest.py\'); foo.myTestFunction()\""); 
    p.start(cmd); 
    qDebug()<< p.arguments(); 
    if (!p.waitForFinished(-1)) 
     qDebug() << "Make failed:" << p.errorString(); 
    else 
     qDebug() << "Make output:" << p.readAll(); 
    qDebug()<<"ran py script"; 
    return a.exec(); 
} 

输出:

如果我们去掉引号
("-c", "import imp; foo = imp.load_source('myTest', '/home/qhipa/myTest.py'); foo.myTestFunction()") 
Make output: "output: this is the python script\n" 
ran py script 

同样的作品,似乎在的开始即使命令需要引号,也不需要放置引号。

QProcess p; 
QStringList params = QStringList()<<"-c"<< 
            "import imp; foo = imp.load_source(\'myTest\', \'/home/qhipa/myTest.py\'); foo.myTestFunction()"; 
p.start("/usr/bin/python2", params); 
qDebug()<< p.arguments(); 
if (!p.waitForFinished(-1)) 
    qDebug() << "Make failed:" << p.errorString(); 
else 
    qDebug() << "Make output:" << p.readAll(); 
qDebug()<<"ran py script"; 
+0

接受“concatenamos”(这是afaik西班牙语变体“we concatenate”) – LandonZeKepitelOfGreytBritn

+0

@trilolil同样适用于删除引号的情况,似乎在参数的开始部分不需要放置引号,即使该命令需要它。 :P – eyllanesc