我用QProcess做了一个简单的例子。
只需从 DBpedia 服务器获取 json 值即可。
Q.如何使用QProcess进行命令?
curl -s "http://dbpedia.org/data/Haeinsa.json"| perl dbpediaPlaceData.txt 'Haeinsa'
我的代码
1. 从数据库百科服务器获取 JSON 值#include <QCoreApplication>
#include <QDebug>
#include <QProcess>
#include <QFileInfo>
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
QString command="curl";
QProcess pro;
QStringList arg;
arg.append("-v");
arg.append("--insecure");
arg.append("GET");
arg.append("http://dbpedia.org/data/Haeinsa.json");
pro.execute(command,arg);
pro.start(command);
pro.waitForStarted();
pro.waitForReadyRead();
pro.waitForFinished();
QByteArray qa=pro.readAll();
QString reply=QString::fromUtf8(qa);
qDebug() << reply;
return a.exec();
}
2. perl dbpediaPlaceData.txt 'Haeinsa' void processPerl()
{
QString command = "perl";
QString fileName ="/Users/yoshimi/dbpediaPlaceData.txt ";
QString cmd = "perl" + fileName + "Haeinsa";
QProcess* process_screen = new QProcess();
process_screen->start(cmd);
process_screen->waitForStarted();
QByteArray q=process_screen->readAll();
QString replyB=QString::fromUtf8(q);
qDebug() << replyB;
}
据我了解,您想使用
QProcess
实现该命令,因此主要困难是实现管道: |
,在这种情况下,您必须使用 setStandardOutputProcess()
:
QProcess curlProcess;
QProcess perlProcess;
QString filename = "/Users/yoshimi/Music/cridb/dbpediaPlaceData/dbpediaPlaceData.txt";
curlProcess.setStandardOutputProcess(&perlProcess);
curlProcess.start("curl", {"-s", "http://dbpedia.org/data/Haeinsa.json"});
if (curlProcess.waitForFinished()){
perlProcess.start("perl", { filename, "Haeinsa"});
if(perlProcess.waitForFinished()){
qDebug()<< perlProcess.readAll();
}
}