我使用QProcess做了一个简单的例子。
只需从DBpedia服务器获取json值即可。
问:如何使用QProcess进行命令?
curl -s "http://dbpedia.org/data/Haeinsa.json"| perl dbpediaPlaceData.txt 'Haeinsa'
#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;
}
答案 0 :(得分:1)
据我了解,您想使用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();
}
}