Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

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
    0_1534343659776_result.png

    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 using startDetached() instead of start() 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 from start() are you expecting it to provide?



  • startDetached() function is easy to access one cmd and arguments.

    If i 'm wrong, I want to know solution..



  • @darongyi
    I'm sorry but I don't know what you mean. In QProcess you could use exec() or start() with much the same arguments as startDetached(). You will not be able to pipe in/out from a detached process, so I don't know how you think your start()ed curl process is going to communicate with your startDetach()ed perl process.

    There is nothing you can accomplish here with startDetach() which you cannot accomplish with start() or exec(), and you have no reason to run either of these processes detached. Up to you.


Log in to reply