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. Regarding QProcess:: Not able to read data from the console

Regarding QProcess:: Not able to read data from the console

Scheduled Pinned Locked Moved Solved General and Desktop
27 Posts 5 Posters 8.7k Views 2 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.
  • Naveen_DN Offline
    Naveen_DN Offline
    Naveen_D
    wrote on last edited by
    #1

    Hi all

    I am using QProcess in my application to start another process which prints some information in the console using qDebug, but i am not able to read the printed data from the console. I tried with the readAllStandardOutput() method but i am getting empty string in my application output.

    can anyone guide me how to do this ?

    code is...
    .cpp

    #include "ProcessWidget.h"
    
    ProcessWidget::ProcessWidget(QWidget *parent)
        : QWidget(parent)
    {
        m_btn= new QPushButton("click here",this);
        connect(m_btn,SIGNAL(clicked()),this,SLOT(vStartProcess()));
    }
    
    void ProcessWidget::vStartProcess()
    {
        QString program = "/home/ubuntu/Documents/Sample_Examples_Qt_Qml/VoiceRecognition/VoiceRecognition";
        myProcess = new QProcess;
        myProcess->start(program);
    //    myProcess->waitForFinished(30000);
        QString output(myProcess->readAllStandardOutput());
        qDebug()<<"Output>>>>"<<output<<endl;
    }
    
    ProcessWidget::~ProcessWidget()
    {
    
    }
    

    Naveen_D

    1 Reply Last reply
    0
    • VRoninV Offline
      VRoninV Offline
      VRonin
      wrote on last edited by VRonin
      #2

      the process tells you when to read, you can't just read at will...
      (you also have memory leak problems)

      void ProcessWidget::vStartProcess()
      {
          const QString program = QStringLiteral("/home/ubuntu/Documents/Sample_Examples_Qt_Qml/VoiceRecognition/VoiceRecognition");
          QProcess* myProcess = new QProcess(this);
          connect(myProcess,&QProcess::readyReadStandardOutput,[myProcess]()->void{
              qDebug()<<"Output: "<< myProcess->readAllStandardOutput();
          });
          connect(myProcess,&QProcess::finished,myProcess,&QProcess::deleteLater);
          myProcess->start(program);
      }
      

      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
      ~Napoleon Bonaparte

      On a crusade to banish setIndexWidget() from the holy land of Qt

      Naveen_DN 1 Reply Last reply
      3
      • m.sueM Offline
        m.sueM Offline
        m.sue
        wrote on last edited by m.sue
        #3

        Hi,
        you can use waitForReadyRead(milliseconds). But usually you connect to the readyReadStandardOutput() signal and readAllStandardOutput() there, see above :-).

        AND you should not forget to connect to readyReadStandardError() as well. There you will see problems like: "VoiceRecognition: command not found".
        -Michael

        1 Reply Last reply
        3
        • VRoninV VRonin

          the process tells you when to read, you can't just read at will...
          (you also have memory leak problems)

          void ProcessWidget::vStartProcess()
          {
              const QString program = QStringLiteral("/home/ubuntu/Documents/Sample_Examples_Qt_Qml/VoiceRecognition/VoiceRecognition");
              QProcess* myProcess = new QProcess(this);
              connect(myProcess,&QProcess::readyReadStandardOutput,[myProcess]()->void{
                  qDebug()<<"Output: "<< myProcess->readAllStandardOutput();
              });
              connect(myProcess,&QProcess::finished,myProcess,&QProcess::deleteLater);
              myProcess->start(program);
          }
          
          Naveen_DN Offline
          Naveen_DN Offline
          Naveen_D
          wrote on last edited by
          #4

          @VRonin Thanks for the reply...
          i tried with whatever u have shown above...it is giving me error

          /home/ubuntu/Documents/Sample_Examples_Qt_Qml/ProcessTest/ProcessWidget.cpp:15: error: capture of non-variable 'ProcessWidget::myProcess'
          connect(myProcess,&QProcess::readyReadStandardOutput,myProcess->void{
          ^

          /home/ubuntu/Documents/Sample_Examples_Qt_Qml/ProcessTest/ProcessWidget.cpp:16: error: 'this' was not captured for this lambda function
          qDebug()<<"Output: "<< myProcess->readAllStandardOutput();
          ^

          /home/ubuntu/Documents/Sample_Examples_Qt_Qml/ProcessTest/ProcessWidget.cpp:18: error: no matching function for call to 'ProcessWidget::connect(QProcess*&, <unresolved overloaded function type>, QProcess*&, void (QObject::*)())'
          connect(myProcess,&QProcess::finished,myProcess,&QProcess::deleteLater);
          ^

          these are the errors which i am getting...

          Naveen_D

          1 Reply Last reply
          0
          • VRoninV Offline
            VRoninV Offline
            VRonin
            wrote on last edited by
            #5

            yes, this is because you have myProcess as a member of your class, remove it from the members and just leave it as a local variable as I did in my code snippet:

            QProcess* myProcess = new QProcess(this);
            

            instead of:

            myProcess = new QProcess;
            

            "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
            ~Napoleon Bonaparte

            On a crusade to banish setIndexWidget() from the holy land of Qt

            Naveen_DN 2 Replies Last reply
            0
            • VRoninV VRonin

              yes, this is because you have myProcess as a member of your class, remove it from the members and just leave it as a local variable as I did in my code snippet:

              QProcess* myProcess = new QProcess(this);
              

              instead of:

              myProcess = new QProcess;
              
              Naveen_DN Offline
              Naveen_DN Offline
              Naveen_D
              wrote on last edited by
              #6

              @VRonin ya its the same code

              #include "ProcessWidget.h"
              
              ProcessWidget::ProcessWidget(QWidget *parent)
                  : QWidget(parent)
              {
                  m_btn= new QPushButton("click here",this);
                  connect(m_btn,SIGNAL(clicked()),this,SLOT(vStartProcess()));
              }
              
              void ProcessWidget::vStartProcess()
              {
                  const QString program = QStringLiteral("/home/ubuntu/Documents/Sample_Examples_Qt_Qml/VoiceRecognition/VoiceRecognition");
                  myProcess = new QProcess(this);
                  connect(myProcess,&QProcess::readyReadStandardOutput,[myProcess]()->void{
                          qDebug()<<"Output: "<< myProcess->readAllStandardOutput();
                      });
                  connect(myProcess,&QProcess::finished,myProcess,&QProcess::deleteLater);
                  myProcess->start(program);
              //    myProcess->waitForFinished(3000);
              //    QString output(myProcess->readAll());
              //    qDebug()<<"Output>>>>"<<output<<endl;
              }
              
              

              for this code i am getting that errors.

              Naveen_D

              VRoninV 1 Reply Last reply
              0
              • VRoninV VRonin

                yes, this is because you have myProcess as a member of your class, remove it from the members and just leave it as a local variable as I did in my code snippet:

                QProcess* myProcess = new QProcess(this);
                

                instead of:

                myProcess = new QProcess;
                
                Naveen_DN Offline
                Naveen_DN Offline
                Naveen_D
                wrote on last edited by Naveen_D
                #7

                @VRonin ya got that....but now i am getting no matching function call for the second connect statement...

                Naveen_D

                1 Reply Last reply
                0
                • Naveen_DN Naveen_D

                  @VRonin ya its the same code

                  #include "ProcessWidget.h"
                  
                  ProcessWidget::ProcessWidget(QWidget *parent)
                      : QWidget(parent)
                  {
                      m_btn= new QPushButton("click here",this);
                      connect(m_btn,SIGNAL(clicked()),this,SLOT(vStartProcess()));
                  }
                  
                  void ProcessWidget::vStartProcess()
                  {
                      const QString program = QStringLiteral("/home/ubuntu/Documents/Sample_Examples_Qt_Qml/VoiceRecognition/VoiceRecognition");
                      myProcess = new QProcess(this);
                      connect(myProcess,&QProcess::readyReadStandardOutput,[myProcess]()->void{
                              qDebug()<<"Output: "<< myProcess->readAllStandardOutput();
                          });
                      connect(myProcess,&QProcess::finished,myProcess,&QProcess::deleteLater);
                      myProcess->start(program);
                  //    myProcess->waitForFinished(3000);
                  //    QString output(myProcess->readAll());
                  //    qDebug()<<"Output>>>>"<<output<<endl;
                  }
                  
                  

                  for this code i am getting that errors.

                  VRoninV Offline
                  VRoninV Offline
                  VRonin
                  wrote on last edited by
                  #8

                  @Naveen_D said in Regarding QProcess:: Not able to read data from the console:

                  ya its the same code

                  No, as mentioned above you are using a member of the class while you should use a local variable

                  now i am getting no matching function call for the second connect statement

                  could you post the error output?

                  "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                  ~Napoleon Bonaparte

                  On a crusade to banish setIndexWidget() from the holy land of Qt

                  Naveen_DN 1 Reply Last reply
                  0
                  • VRoninV VRonin

                    @Naveen_D said in Regarding QProcess:: Not able to read data from the console:

                    ya its the same code

                    No, as mentioned above you are using a member of the class while you should use a local variable

                    now i am getting no matching function call for the second connect statement

                    could you post the error output?

                    Naveen_DN Offline
                    Naveen_DN Offline
                    Naveen_D
                    wrote on last edited by
                    #9

                    @VRonin ya i came to know about the local variable.
                    the error is...
                    /home/ubuntu/Documents/Sample_Examples_Qt_Qml/ProcessTest/ProcessWidget.cpp:18: error: no matching function for call to 'ProcessWidget::connect(QProcess*&, <unresolved overloaded function type>, QProcess*&, void (QObject::*)())'
                    connect(myProcess,&QProcess::finished,myProcess,&QProcess::deleteLater);
                    ^

                    Naveen_D

                    1 Reply Last reply
                    0
                    • VRoninV Offline
                      VRoninV Offline
                      VRonin
                      wrote on last edited by
                      #10

                      I did not realise finished is overloaded:

                      connect(myProcess,static_cast<void (QProcess::*)(int , QProcess::ExitStatus)>(&QProcess::finished),myProcess,&QProcess::deleteLater);
                      

                      or

                      connect(myProcess,SIGNAL(finished()),myProcess,SLOT(deleteLater()));
                      

                      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                      ~Napoleon Bonaparte

                      On a crusade to banish setIndexWidget() from the holy land of Qt

                      Naveen_DN 1 Reply Last reply
                      1
                      • VRoninV VRonin

                        I did not realise finished is overloaded:

                        connect(myProcess,static_cast<void (QProcess::*)(int , QProcess::ExitStatus)>(&QProcess::finished),myProcess,&QProcess::deleteLater);
                        

                        or

                        connect(myProcess,SIGNAL(finished()),myProcess,SLOT(deleteLater()));
                        
                        Naveen_DN Offline
                        Naveen_DN Offline
                        Naveen_D
                        wrote on last edited by
                        #11

                        @VRonin said in Regarding QProcess:: Not able to read data from the console:

                        connect(myProcess,SIGNAL(finished()),myProcess,SLOT(deleteLater()));

                        i used this connect statement...after this the prgm builds without any issues but i am not getting any output in the console...

                        Naveen_D

                        1 Reply Last reply
                        0
                        • VRoninV Offline
                          VRoninV Offline
                          VRonin
                          wrote on last edited by
                          #12

                          as @m-sue suggested connect the standard error too:

                          connect(myProcess,&QProcess::readyReadStandardError,[myProcess]()->void{
                                  qDebug()<<"Output: "<< myProcess->readAllStandardError();
                              });
                          

                          "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                          ~Napoleon Bonaparte

                          On a crusade to banish setIndexWidget() from the holy land of Qt

                          Naveen_DN 1 Reply Last reply
                          2
                          • VRoninV VRonin

                            as @m-sue suggested connect the standard error too:

                            connect(myProcess,&QProcess::readyReadStandardError,[myProcess]()->void{
                                    qDebug()<<"Output: "<< myProcess->readAllStandardError();
                                });
                            
                            Naveen_DN Offline
                            Naveen_DN Offline
                            Naveen_D
                            wrote on last edited by
                            #13

                            @VRonin said in Regarding QProcess:: Not able to read data from the console:

                            connect(myProcess,&QProcess::readyReadStandardError,myProcess->void{
                            qDebug()<<"Output: "<< myProcess->readAllStandardError();
                            });

                            yes...got the output
                            thanks. one more doubt after getting the output, when i close my application i get the following in my console..
                            QProcess: Destroyed while process ("/home/ubuntu/Documents/Sample_Examples_Qt_Qml/VoiceRecognition/VoiceRecognition") is still running.
                            is there any other way to end the process ??

                            Naveen_D

                            FlotisableF 1 Reply Last reply
                            0
                            • Naveen_DN Naveen_D

                              @VRonin said in Regarding QProcess:: Not able to read data from the console:

                              connect(myProcess,&QProcess::readyReadStandardError,myProcess->void{
                              qDebug()<<"Output: "<< myProcess->readAllStandardError();
                              });

                              yes...got the output
                              thanks. one more doubt after getting the output, when i close my application i get the following in my console..
                              QProcess: Destroyed while process ("/home/ubuntu/Documents/Sample_Examples_Qt_Qml/VoiceRecognition/VoiceRecognition") is still running.
                              is there any other way to end the process ??

                              FlotisableF Offline
                              FlotisableF Offline
                              Flotisable
                              wrote on last edited by
                              #14

                              @Naveen_D
                              I think you can connect ProcessWidget's destroyed signal to myProcess's close or kill slot

                              1 Reply Last reply
                              0
                              • VRoninV Offline
                                VRoninV Offline
                                VRonin
                                wrote on last edited by
                                #15

                                remove the parent:
                                new QProcess(this); should become new QProcess; and then add:
                                connect(this,&ProcessWidget::destroyed,myProcess,&QProcess::kill);

                                "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                                ~Napoleon Bonaparte

                                On a crusade to banish setIndexWidget() from the holy land of Qt

                                Naveen_DN 1 Reply Last reply
                                1
                                • VRoninV VRonin

                                  remove the parent:
                                  new QProcess(this); should become new QProcess; and then add:
                                  connect(this,&ProcessWidget::destroyed,myProcess,&QProcess::kill);

                                  Naveen_DN Offline
                                  Naveen_DN Offline
                                  Naveen_D
                                  wrote on last edited by Naveen_D
                                  #16

                                  @VRonin if there are some huge debug statements and if i want a particular debug statement out of that means...now i am getting complete debug statements in my console...i want to choose a particular statement out of that. how to do this..?

                                  Naveen_D

                                  1 Reply Last reply
                                  0
                                  • VRoninV Offline
                                    VRoninV Offline
                                    VRonin
                                    wrote on last edited by
                                    #17

                                    If what you are reading are strings then you can manipulate them the usual way, for example:

                                    const QString processOut = QString::fromLatin1(myProcess->readAllStandardError());
                                    const QRegularExpression regXp("MyField: (\\S+)");
                                    const auto regXpMatch = regXp.match(processOut);
                                    if(regXpMatch.hasMatch()){
                                    qDebug() << "Output: "<< regXpMatch.captured(1);
                                    }
                                    

                                    "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                                    ~Napoleon Bonaparte

                                    On a crusade to banish setIndexWidget() from the holy land of Qt

                                    Naveen_DN 1 Reply Last reply
                                    0
                                    • VRoninV VRonin

                                      If what you are reading are strings then you can manipulate them the usual way, for example:

                                      const QString processOut = QString::fromLatin1(myProcess->readAllStandardError());
                                      const QRegularExpression regXp("MyField: (\\S+)");
                                      const auto regXpMatch = regXp.match(processOut);
                                      if(regXpMatch.hasMatch()){
                                      qDebug() << "Output: "<< regXpMatch.captured(1);
                                      }
                                      
                                      Naveen_DN Offline
                                      Naveen_DN Offline
                                      Naveen_D
                                      wrote on last edited by Naveen_D
                                      #18

                                      @VRonin sorry i didn't get the the regular exp part...
                                      the actual scenario is, i will give the input at runtime via live microphone when the other process is started by this current Qprocess...and in that started process i am storing the input in a string and displaying through qDebug. those debug statements from the started process are now copied to my console, from which i need to take a particular string and pass it through a signal.

                                      Naveen_D

                                      VRoninV 1 Reply Last reply
                                      0
                                      • Naveen_DN Naveen_D

                                        @VRonin sorry i didn't get the the regular exp part...
                                        the actual scenario is, i will give the input at runtime via live microphone when the other process is started by this current Qprocess...and in that started process i am storing the input in a string and displaying through qDebug. those debug statements from the started process are now copied to my console, from which i need to take a particular string and pass it through a signal.

                                        VRoninV Offline
                                        VRoninV Offline
                                        VRonin
                                        wrote on last edited by VRonin
                                        #19

                                        @Naveen_D said in Regarding QProcess:: Not able to read data from the console:

                                        which i need to take a particular string

                                        I don't know how you identify the particular string so I just guessed a use based off a regular expression. what I mean is that instead of passing whatever you receive to your console with qDebug()<<"Output: "<< myProcess->readAllStandardOutput(); you can do whatever you want with the read data. For example, if you want to pass every line you receive to a signal you can use:

                                        connect(myProcess,&QProcess::readyReadStandardError,[myProcess,this]()->void{
                                        const QByteArray readData= myProcess->readAllStandardError();
                                        QTextStream lineReader(readData);
                                        QString tempLine;
                                        while (lineReader.readLineInto(&tempLine)) {
                                        /*emit*/ mySignal(tempLine);
                                        }
                                                });
                                        

                                        "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                                        ~Napoleon Bonaparte

                                        On a crusade to banish setIndexWidget() from the holy land of Qt

                                        Naveen_DN 1 Reply Last reply
                                        0
                                        • VRoninV VRonin

                                          @Naveen_D said in Regarding QProcess:: Not able to read data from the console:

                                          which i need to take a particular string

                                          I don't know how you identify the particular string so I just guessed a use based off a regular expression. what I mean is that instead of passing whatever you receive to your console with qDebug()<<"Output: "<< myProcess->readAllStandardOutput(); you can do whatever you want with the read data. For example, if you want to pass every line you receive to a signal you can use:

                                          connect(myProcess,&QProcess::readyReadStandardError,[myProcess,this]()->void{
                                          const QByteArray readData= myProcess->readAllStandardError();
                                          QTextStream lineReader(readData);
                                          QString tempLine;
                                          while (lineReader.readLineInto(&tempLine)) {
                                          /*emit*/ mySignal(tempLine);
                                          }
                                                  });
                                          
                                          Naveen_DN Offline
                                          Naveen_DN Offline
                                          Naveen_D
                                          wrote on last edited by
                                          #20

                                          @VRonin As said by you i am trying with Regular Exp,
                                          the output at present i am getting is ...
                                          Output: "loaded file >>>>>>> 0x1c57220 \n\n"
                                          Output: "Recognition instance >>>>> 0x1c58660 \n\n"
                                          Output: "\n<<< please speak >>> \n\n \n\n"
                                          Output: "Sentence :\nchar *data>>>>>>> <s> \n\nchar *data>>>>>>> WORLD \n\nchar *data>>>>>>> </s> \n\nReceived Command: "WORLD" \n\n"

                                          from this output i want to select the part which is in bold... but when i run my program and give output through a microphone i get the above output in my console...but i am not getting the regular exp qDebug part.

                                          Naveen_D

                                          1 Reply Last reply
                                          0

                                          • Login

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