I try to use QProcess::startDetached() funtion.
-
I made command work using QProcess.
curl "http://dbpedia.org/data/Haeinsa.json" | perl dbpediaPlaceData.txt 'Haeinsa'
curl bring json value well,but perl command don't work.
qt_run_command.h
#include <QObject> #include <QDebug> #include <QProcess> #include <memory> template<typename... Args> struct SELECT { template<typename C, typename R> static constexpr auto OVERLOAD_OF( R (C::*pmf)(Args...) ) -> decltype(pmf) { return pmf; } }; template<typename CommandCallback> QProcess* runCommand(QString command, QStringList args,CommandCallback commandCallback) { qDebug() << "Run command:" << command << " Args:" << args; QProcess *process = new QProcess(); QByteArray cmdBuff; auto conn = std::make_shared<QMetaObject::Connection>(); *conn = QObject::connect(process, SELECT<int>::OVERLOAD_OF(&QProcess::finished), [commandCallback,conn, process] (int exitCode) { QObject::disconnect(*conn); qDebug() << "Reading Results"; QByteArray buffer; while(process->canReadLine()){ buffer.append(process->readAllStandardOutput()); qDebug() << buffer; } qDebug() << "Done Reading Results"; commandCallback(QString(buffer.toStdString().c_str())); }); // process->start(command, args); //curl \"http://dbpedia.org/data/Haeinsa.json\"" //| perl dbpediaPlaceData.txt 'Haeinsa' // curl "http://dbpedia.org/data/Haeinsa.json" | perl dbpediaPlaceData.txt 'Haeinsa' QString filename = "/Users/yoshimi/Music/cridb/dbpediaPlaceData/dbpediaPlaceData.txt"; QString perl_string = QString("perl %1 \"Haeinsa\"").arg(filename); process->startDetached(command, QStringList() << "http://dbpedia.org/data/Haeinsa.json" << " | " << perl_string); return process; }
main.cpp
#include "qt_run_command.h" int main(int argc, char *argv[]) { QCoreApplication a(argc, argv); QByteArray strReadOut; QString filename = "/Users/yoshimi/Music/cridb/dbpediaPlaceData/dbpediaPlaceData.txt"; //usage QProcess *process = runCommand("curl" , QStringList() << "", [&strReadOut, &process](QString results) { qDebug() << "Run command results:" << results; strReadOut += results; delete process; }); qDebug() << "ByteArray" << strReadOut; return a.exec(); }
Result
original result
yoshimi~/Music/cridb/dbpediaPlaceData$ curl "http://dbpedia.org/data/Haeinsa.json" | perl dbpediaPlaceData.txt 'Haeinsa'{"command":"save","record_type":"thing","data":{"lat":35.7999992370605,"subtype":"place","name":"Haeinsa","notes":"wkpd-Haeinsa","long":128.100006103516}}
-
@darongyi
It's difficult to tell what you are trying to do, but it is most unlikely that usingstartDetached()
instead ofstart()
will make any difference to whatever your problem is, and may well make it worse.What is the relevance of
startDetached()
to all of this? Why are you trying to use it, what difference fromstart()
are you expecting it to provide? -
@darongyi
I'm sorry but I don't know what you mean. InQProcess
you could useexec()
orstart()
with much the same arguments asstartDetached()
. You will not be able to pipe in/out from a detached process, so I don't know how you think yourstart()
edcurl
process is going to communicate with yourstartDetach()
edperl
process.There is nothing you can accomplish here with
startDetach()
which you cannot accomplish withstart()
orexec()
, and you have no reason to run either of these processes detached. Up to you.