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. Qprocess reading execution output Simentensously
QtWS25 Last Chance

Qprocess reading execution output Simentensously

Scheduled Pinned Locked Moved Solved General and Desktop
qprocessqtcorecommand lineshell
9 Posts 4 Posters 2.4k Views
  • 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.
  • A Offline
    A Offline
    Amirhos
    wrote on last edited by Amirhos
    #1

    Hi
    I am trying to run 3party(a flasher tool) app throw my qt application using qprocess class.
    The qprocess class offer readAllStandardOutput function for reading application output
    But the problem is , the application should end to
    readyReadStandardOutput signal emitted.
    I want to read application output Simentensously.
    Assuming that you want to use ping command to ping
    Google and logging each packet has send on your qt application.

    How i supposed to do that?

    jsulmJ 1 Reply Last reply
    0
    • A Amirhos

      Hi
      I am trying to run 3party(a flasher tool) app throw my qt application using qprocess class.
      The qprocess class offer readAllStandardOutput function for reading application output
      But the problem is , the application should end to
      readyReadStandardOutput signal emitted.
      I want to read application output Simentensously.
      Assuming that you want to use ping command to ping
      Google and logging each packet has send on your qt application.

      How i supposed to do that?

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @Amirhos said in Qprocess reading execution output Simentensously:

      But the problem is , the application should end to
      readyReadStandardOutput signal has emitted.

      I don't understand this. Can you rephrase? What exactly happens and what exactly do you want to happen?

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • JonBJ Offline
        JonBJ Offline
        JonB
        wrote on last edited by
        #3

        Just connect QProcess::readyReadStandardOutput signal to a slot which calls readAll. It will be called repeatedly, each time any new output arrives. And do not use any of synchronous the waitFor... calls, keep everything asynchronous.

        1 Reply Last reply
        1
        • A Offline
          A Offline
          Amirhos
          wrote on last edited by Amirhos
          #4

          @JonB and @jsulm
          here is my code

          QProcess *proc;
          void MainWindow::on_pushButton_clicked()
          {
              proc=new QProcess(this);
              proc->connect(proc,&QProcess::readyReadStandardOutput,[=]{
                  qDebug()<<"new Data:";
                  qDebug().noquote()<<proc->readAll();
              });
              proc->start("test.exe");
              
          }
          

          and test.exe

          #include <stdio.h>
          int main() {
          
              char buff[100];
              printf("Enter Arg #1:\n");
              gets(buff);
              printf("Arg #1 is %s\n",buff);
          
              printf("Enter Arg #2:\n");
              gets(buff);
              printf("Arg #2 is %s\n",buff);
          
              printf("Enter Arg #2:\n");
              gets(buff);
              printf("Arg #2 is %s\n",buff);
              return 0;
          }
          

          i realize that ping command actually work fine and here the output:

          new Data:
          Pinging google.com [172.217.18.142] with 32 bytes of data:
          new Data:
          Reply from 172.217.18.142: bytes=32 time=83ms TTL=115
          new Data:
          Reply from 172.217.18.142: bytes=32 time=72ms TTL=115
          new Data:
          Reply from 172.217.18.142: bytes=32 time=72ms TTL=115
          new Data:
          Reply from 172.217.18.142: bytes=32 time=71ms TTL=115
          Ping statistics for 172.217.18.142:
              Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),
          Approximate round trip times in milli-seconds:
              Minimum = 71ms, Maximum = 83ms, Average = 74ms
          

          but test.exe doesn't work
          I figure out i Should use proc->write() three times to readyReadStandardOutput signal emitted.(=> test.exe should return 0).so basically i can't communicate with test.exe interactively.

          1 Reply Last reply
          0
          • SGaistS Offline
            SGaistS Offline
            SGaist
            Lifetime Qt Champion
            wrote on last edited by
            #5

            Hi,

            You do not do any error checking. You should first check that your application starts properly and then communicate with it.

            On a side note, using a static QProcess pointer is not a good idea. Just make it a member variable.

            Interested in AI ? www.idiap.ch
            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

            A 1 Reply Last reply
            1
            • SGaistS SGaist

              Hi,

              You do not do any error checking. You should first check that your application starts properly and then communicate with it.

              On a side note, using a static QProcess pointer is not a good idea. Just make it a member variable.

              A Offline
              A Offline
              Amirhos
              wrote on last edited by Amirhos
              #6

              @SGaist

              I add these signals to my code

                  proc->connect(proc,&QProcess::started,[proc]{
                      qDebug()<<"started";
              
                  });
                  proc->connect(proc,&QProcess::stateChanged,[proc](QProcess::ProcessState state){
                      qDebug()<<"new State: "<<state;
                  });
                  proc->connect(proc,&QProcess::errorOccurred,[proc](QProcess::ProcessError error){
                      qDebug()<<"Error: "<<error;
                  });
              

              and change proc to a member variable

              then I try to run test.exe
              output:

              new State:  QProcess::Starting
              new State:  QProcess::Running
              started
              

              looks like test.exe is running but is waiting for input.

              JonBJ 1 Reply Last reply
              0
              • A Amirhos

                @SGaist

                I add these signals to my code

                    proc->connect(proc,&QProcess::started,[proc]{
                        qDebug()<<"started";
                
                    });
                    proc->connect(proc,&QProcess::stateChanged,[proc](QProcess::ProcessState state){
                        qDebug()<<"new State: "<<state;
                    });
                    proc->connect(proc,&QProcess::errorOccurred,[proc](QProcess::ProcessError error){
                        qDebug()<<"Error: "<<error;
                    });
                

                and change proc to a member variable

                then I try to run test.exe
                output:

                new State:  QProcess::Starting
                new State:  QProcess::Running
                started
                

                looks like test.exe is running but is waiting for input.

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

                @Amirhos said in Qprocess reading execution output Simentensously:

                looks like test.exe is running but is waiting for input.

                Yes, you have gets() in the child process.

                I figure out i Should use proc->write() three times to readyReadStandardOutput signal emitted.(=> test.exe should return 0).so basically i can't communicate with test.exe interactively.

                Yes you need to proc->write() to send some data there. What do you mean by "i can't communicate with test.exe interactively."?

                I don't understand what your issue is? Oh, put a ffllush(stdout) into child if you expect to first see its printf("Enter Arg #1:\n");, is that what you mean? (And to start with remove the child's gets() while you verify your parent gets its output.)

                A 1 Reply Last reply
                2
                • JonBJ JonB

                  @Amirhos said in Qprocess reading execution output Simentensously:

                  looks like test.exe is running but is waiting for input.

                  Yes, you have gets() in the child process.

                  I figure out i Should use proc->write() three times to readyReadStandardOutput signal emitted.(=> test.exe should return 0).so basically i can't communicate with test.exe interactively.

                  Yes you need to proc->write() to send some data there. What do you mean by "i can't communicate with test.exe interactively."?

                  I don't understand what your issue is? Oh, put a ffllush(stdout) into child if you expect to first see its printf("Enter Arg #1:\n");, is that what you mean? (And to start with remove the child's gets() while you verify your parent gets its output.)

                  A Offline
                  A Offline
                  Amirhos
                  wrote on last edited by Amirhos
                  #8

                  @JonB said in Qprocess reading execution output Simentensously:

                  I don't understand what your issue is? Oh, put a ffllush(stdout) into child if you expect to first see its printf("Enter Arg #1:\n");

                  yes this is exactly what i want . thank u @JonB
                  ffllush(stdout) work fine for me.
                  but I think my 3party application doesn't use this method.
                  is there any other way to do this ?

                  JonBJ 1 Reply Last reply
                  0
                  • A Amirhos

                    @JonB said in Qprocess reading execution output Simentensously:

                    I don't understand what your issue is? Oh, put a ffllush(stdout) into child if you expect to first see its printf("Enter Arg #1:\n");

                    yes this is exactly what i want . thank u @JonB
                    ffllush(stdout) work fine for me.
                    but I think my 3party application doesn't use this method.
                    is there any other way to do this ?

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

                    @Amirhos
                    Unfortunately this is difficult, especially under Windows. If, for whatever reason, the third-party app is buffering its output at its own side then you cannot force it down the pipe to your parent, it depends how the code is written at their side....

                    1 Reply Last reply
                    2

                    • Login

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