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 show stdoutput only on ending of excecution.

Qprocess show stdoutput only on ending of excecution.

Scheduled Pinned Locked Moved Unsolved General and Desktop
23 Posts 6 Posters 7.9k 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.
  • S saran

    I 'm using QProcess to launch a external application. (st-link_cli.exe)
    if I excecute the application on cmd.exe, I can see the progress of the software but when I excecute on Qprocess I have a signal only on end off execution and I can't show the progress of the app.
    There is another signal that can I use?

            process = new QProcess(this);
            QStringList argumentos;
            process->setWorkingDirectory(QDir::home().absolutePath());
            QString file = "ST-LINK_CLI.exe";
            qWarning(file.toStdString().c_str());
            argumentos << "-P" << filehex  << "-Q" << "-V" <<  "after_programming";
            process->setReadChannel(QProcess::StandardOutput);
            process->setProcessChannelMode(QProcess::MergedChannels);
            process->setCurrentReadChannel(QProcess::StandardOutput);
            connect(process, SIGNAL(readyReadStandardOutput()), this, SLOT(readOutput()));
            connect(process, SIGNAL(readyRead()), this, SLOT(readOutput()));
            connect(process, SIGNAL(finished(int,QProcess::ExitStatus)),this,SLOT(stlinkFinish()));
            process->start(file,argumentos);
    
    jsulmJ Offline
    jsulmJ Offline
    jsulm
    Lifetime Qt Champion
    wrote on last edited by
    #11

    @saran said in Qprocess show stdoutput only on ending of excecution.:

    process->start(file,argumentos);

    Can you show what you're doing after this line?

    Also why do you have this

    process->setReadChannel(QProcess::StandardOutput);
    process->setProcessChannelMode(QProcess::MergedChannels);
    process->setCurrentReadChannel(QProcess::StandardOutput);
    

    ?

    What are you doing in readOutput()?

    One more question: to which channel does this executable write (stdout or stderr)?

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

    JonBJ S 2 Replies Last reply
    0
    • jsulmJ jsulm

      @saran said in Qprocess show stdoutput only on ending of excecution.:

      process->start(file,argumentos);

      Can you show what you're doing after this line?

      Also why do you have this

      process->setReadChannel(QProcess::StandardOutput);
      process->setProcessChannelMode(QProcess::MergedChannels);
      process->setCurrentReadChannel(QProcess::StandardOutput);
      

      ?

      What are you doing in readOutput()?

      One more question: to which channel does this executable write (stdout or stderr)?

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

      @jsulm said in Qprocess show stdoutput only on ending of excecution.:

      One more question: to which channel does this executable write (stdout or stderr)?

      Because he is using setProcessChannelMode(QProcess::MergedChannels) the executable will effectively only write to stdout, not stderr. http://doc.qt.io/qt-5/qprocess.html:

      [QProcess::MergedChannels] QProcess merges the output of the running process into the standard output channel (stdout). The standard error channel (stderr) will not receive any data. The standard output and standard error data of the running process are interleaved.

      1 Reply Last reply
      1
      • jsulmJ jsulm

        @saran said in Qprocess show stdoutput only on ending of excecution.:

        process->start(file,argumentos);

        Can you show what you're doing after this line?

        Also why do you have this

        process->setReadChannel(QProcess::StandardOutput);
        process->setProcessChannelMode(QProcess::MergedChannels);
        process->setCurrentReadChannel(QProcess::StandardOutput);
        

        ?

        What are you doing in readOutput()?

        One more question: to which channel does this executable write (stdout or stderr)?

        S Offline
        S Offline
        saran
        wrote on last edited by
        #13

        @jsulm
        process->start is the last line.
        I have

        process->setReadChannel(QProcess::StandardOutput);
        process->setProcessChannelMode(QProcess::MergedChannels);
        process->setCurrentReadChannel(QProcess::StandardOutput);
        

        Just to make sure the process writes to stdout but anyway, without those lines the behavior is the same.

        On readOutput I have:

        void softsWindow::readOutput(){
            qWarning(QString::number(process->bytesAvailable()).toStdString().c_str());
            while(process->canReadLine()){
                QByteArray linea = process->readLine();
                ui->textBrowser->append(linea);        
            }
        } 
        

        As you can see, the function directs the text of the stdout to a QTextBrowser
        With the qWarning(QString::number(process->bytesAvailable()).toStdString().c_str()); line, I can see that the function receives all the data at once. the line print a "677" on application output, at same time the QTextBrowser print all the output.

        JonBJ 1 Reply Last reply
        1
        • S saran

          @jsulm
          process->start is the last line.
          I have

          process->setReadChannel(QProcess::StandardOutput);
          process->setProcessChannelMode(QProcess::MergedChannels);
          process->setCurrentReadChannel(QProcess::StandardOutput);
          

          Just to make sure the process writes to stdout but anyway, without those lines the behavior is the same.

          On readOutput I have:

          void softsWindow::readOutput(){
              qWarning(QString::number(process->bytesAvailable()).toStdString().c_str());
              while(process->canReadLine()){
                  QByteArray linea = process->readLine();
                  ui->textBrowser->append(linea);        
              }
          } 
          

          As you can see, the function directs the text of the stdout to a QTextBrowser
          With the qWarning(QString::number(process->bytesAvailable()).toStdString().c_str()); line, I can see that the function receives all the data at once. the line print a "677" on application output, at same time the QTextBrowser print all the output.

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

          @saran
          Do two things:

          1. Replace while(process->canReadLine()) and the process->readLine(); by QProcess::readAllStandardOutput(). It avoids looping and relying on when process->canReadLine() returns true. (It also means we can try "unbufferred", which canReadLine() says it cannot handle; you might want to retry QIODevice::Unbuffered to see if that now makes any difference.) I would think about doing it like that anyway, unless you have reason to want to only grab whole lines at a time for output.

          2. Replace the command you're executing by one which produces voluminous output, e.g. dir /s c:\. What arrives when? Does the subprocess actually get stuck because the parent has failed to read its output as it goes along, which is what will happen if the signal only ever arrives at the end? We need to know whether behaviour is linked to
            how st-link_cli.exe produces its output versus anything else.

          BTW, if when you last ran it returned just "677" as you say, that does not correspond at all to the output you show in your screenshot. Please try to keep things consistent since we are measuring when output arrives.

          S 1 Reply Last reply
          0
          • JonBJ JonB

            @saran
            Do two things:

            1. Replace while(process->canReadLine()) and the process->readLine(); by QProcess::readAllStandardOutput(). It avoids looping and relying on when process->canReadLine() returns true. (It also means we can try "unbufferred", which canReadLine() says it cannot handle; you might want to retry QIODevice::Unbuffered to see if that now makes any difference.) I would think about doing it like that anyway, unless you have reason to want to only grab whole lines at a time for output.

            2. Replace the command you're executing by one which produces voluminous output, e.g. dir /s c:\. What arrives when? Does the subprocess actually get stuck because the parent has failed to read its output as it goes along, which is what will happen if the signal only ever arrives at the end? We need to know whether behaviour is linked to
              how st-link_cli.exe produces its output versus anything else.

            BTW, if when you last ran it returned just "677" as you say, that does not correspond at all to the output you show in your screenshot. Please try to keep things consistent since we are measuring when output arrives.

            S Offline
            S Offline
            saran
            wrote on last edited by
            #15

            @JNBarchan
            When I said that I get "677", I was talking about the qWarning(QString::number(process->bytesAvailable()).toStdString().c_str()); line, this is consistent, when readOutput function is executed, all stdout bytes are availables for read, so process->bytesAvailable() is 677, that is the characters count.
            The screenshot shows the output when I run st-link_cli.exe on cmd.exe, as astated above.

            JonBJ 1 Reply Last reply
            0
            • S saran

              @JNBarchan
              When I said that I get "677", I was talking about the qWarning(QString::number(process->bytesAvailable()).toStdString().c_str()); line, this is consistent, when readOutput function is executed, all stdout bytes are availables for read, so process->bytesAvailable() is 677, that is the characters count.
              The screenshot shows the output when I run st-link_cli.exe on cmd.exe, as astated above.

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

              @saran
              OIC, that's because it has 677 bytes initially, then it manages to keep satisfying the canReadLine with more data which has arrived since, I guess. WE could have done with another qWarning(QString::number(process->bytesAvailable()).toStdString().c_str()); inside the loop.

              Never mind about that. Suggest you try my 2 recommendations and report back :)

              S 1 Reply Last reply
              0
              • JonBJ JonB

                @saran
                OIC, that's because it has 677 bytes initially, then it manages to keep satisfying the canReadLine with more data which has arrived since, I guess. WE could have done with another qWarning(QString::number(process->bytesAvailable()).toStdString().c_str()); inside the loop.

                Never mind about that. Suggest you try my 2 recommendations and report back :)

                S Offline
                S Offline
                saran
                wrote on last edited by saran
                #17

                @JNBarchan

                1. The same with the replaces. The problem is in the SIGNAL, that is not generating the trigger on each new line.

                2. I tried the following and the code works as I expected:

                    QString file = "ping";
                    QStringList argumentos << "192.168.0.1";
                    process->start(file,argumentos);
                

                Result: 5 SIGNAL(readyReadStandardOutput()), on each one, the readOutoput function printed correctly the output on the QTextBrowser.

                So the problem is the st-link_cli.exe output.

                JonBJ 1 Reply Last reply
                0
                • S saran

                  @JNBarchan

                  1. The same with the replaces. The problem is in the SIGNAL, that is not generating the trigger on each new line.

                  2. I tried the following and the code works as I expected:

                      QString file = "ping";
                      QStringList argumentos << "192.168.0.1";
                      process->start(file,argumentos);
                  

                  Result: 5 SIGNAL(readyReadStandardOutput()), on each one, the readOutoput function printed correctly the output on the QTextBrowser.

                  So the problem is the st-link_cli.exe output.

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

                  @saran
                  Yes, like it's being buffered there so you don't get it, that's the usual problem I suspected at start. Try the QIODevice::Unbuffered now assuming you've changed to use QProcess::readAllStandardOutput() instead of process->readLine(); ?

                  S 1 Reply Last reply
                  0
                  • JonBJ JonB

                    @saran
                    Yes, like it's being buffered there so you don't get it, that's the usual problem I suspected at start. Try the QIODevice::Unbuffered now assuming you've changed to use QProcess::readAllStandardOutput() instead of process->readLine(); ?

                    S Offline
                    S Offline
                    saran
                    wrote on last edited by
                    #19

                    @JNBarchan
                    There is no SIGNAL(readyReadStandardOutput() nor SIGNAL(readyRead() when I put QIODevice::Unbuffered .

                    JonBJ 1 Reply Last reply
                    0
                    • S saran

                      @JNBarchan
                      There is no SIGNAL(readyReadStandardOutput() nor SIGNAL(readyRead() when I put QIODevice::Unbuffered .

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

                      @saran
                      Hmm, that sounds odd to me, why would that be?

                      1 Reply Last reply
                      0
                      • J Offline
                        J Offline
                        jdascenzio
                        wrote on last edited by
                        #21

                        I have the same trouble. It's seems to happen when the end of line send by a process is only \n instead of \r\n for windows.....

                        G 1 Reply Last reply
                        0
                        • J jdascenzio

                          I have the same trouble. It's seems to happen when the end of line send by a process is only \n instead of \r\n for windows.....

                          G Offline
                          G Offline
                          greenpitaya
                          wrote on last edited by
                          #22

                          Also facing the same issues, ping works fine realtime, but the readyRead signal isnt getting triggered when theres a 'print' command on my custom executable. As everyone else, it gets triggered only after the process is completed.

                          @saran were you able to debug this problem eventually?

                          @jdascenzio does using \r \n solve this problem? It did not for me

                          1 Reply Last reply
                          0
                          • J Offline
                            J Offline
                            jarl93rsa
                            wrote on last edited by
                            #23

                            Yes I realise this topic is old, but replying to it anyway as it's one of the first things that appear in google when you search this issue.

                            For anyone who hasn't resolved this there's a chance your application is buffering its stdout, and it's not a problem on the qt side.

                            I had this same issue, my logging functions writing to stdout in the application I was trying to get output from wouldn't show until the process had finished. It was resolved by flushing stdout.

                            In my case my c program log function would write to printf(), and at the end of those functions I needed to put fflush(stdout).

                            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