platform: windows10
after I use the start function of QProcess to execute the local (current directory) python file, I can"t get standard output from readAllStandardOutput.
Code of python file:
print "hello,world"
Qt Code:
-sharpinclude <QProcess>
-sharpinclude <QStringList>
-sharpinclude <QByteArray>
-sharpinclude <QDebug>
void fun1(){
QProcess process;
process.start("python test.py");
process.waitForFinished();
QByteArray a = process.readAllStandardOutput();
qDebug()<<a;
}
int main(){
fun();
}
output an only "", no data, but run the python file in cmd, yes, and you can output "hello,world".
when I use the process.execute function to execute the python file directly, the python file executes normally and can output "hello,world" to the Qt terminal.
-sharpinclude <QProcess>
-sharpinclude <QStringList>
-sharpinclude <QByteArray>
-sharpinclude <iostream>
-sharpinclude <QDebug>
void fun2(){
QStringList args("F:/test.py");
QProcess process;
process.execute(QString("Python.exe"), args);
QByteArray a = process.readAllStandardOutput();
process.waitForFinished();
qDebug()<<a;
}
int main(){
fun1();
qDebug<<"--------";
fun2();
}
the execution result is shown in the figure:
I use CPP to execute python script because I want to use python"s requests module to access http url directly. CPP seems to be only able to access socket,url. It seems that it is not easy to do it, so I came up with this trick. If God has a better way, please let me know.
turn to the god to point out the mistake! It would be better if there was a solution.