Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. I try to use QProcess::startDetached() funtion.
Forum Updated to NodeBB v4.3 + New Features

I try to use QProcess::startDetached() funtion.

Scheduled Pinned Locked Moved Solved General and Desktop
4 Posts 2 Posters 1.0k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • darongyiD Offline
    darongyiD Offline
    darongyi
    wrote on last edited by
    #1

    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}}
    
    JonBJ 1 Reply Last reply
    0
    • darongyiD darongyi

      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}}
      
      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by
      #2

      @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?

      1 Reply Last reply
      0
      • darongyiD Offline
        darongyiD Offline
        darongyi
        wrote on last edited by
        #3

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

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

        JonBJ 1 Reply Last reply
        0
        • darongyiD darongyi

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

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

          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by
          #4

          @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.

          1 Reply Last reply
          1

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved