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

Qprocess reading execution output Simentensously



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


  • Lifetime Qt Champion

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



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



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


  • Lifetime Qt Champion

    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.



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



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



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



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