Uploaded image for project: 'Qt'
  1. Qt
  2. QTBUG-30843

QProcess::readAllStandardOutput() in use with QProcess::QProcess:readyReadStandardOutput() does not work as expected

    XMLWordPrintable

Details

    • Bug
    • Resolution: Done
    • P2: Important
    • 5.1.0 Beta 1
    • 5.0.2
    • Core: I/O
    • None
    • Windows 7 Sp1 64 bit, Visual Studio 2010, Qt 5.0.2 for msvc 2010
    • 28ee554b37be39c03c231e7b857f71163dc6ea73

    Description

      A call to QProcess::readAllStandardOutput() after a process has finished does not return the remaining buffered data in the output buffer if it was called earlier during QProcess::readyReadStandardOutput().

      Test-Setup:

      • A QProcess instance (on heap) running a simple batch file (prints numbers from 1 to 10000)
      • A slot named readOutput() connected to QProcess:readyReadStandardOutput()
      • A slot named finished() connected to QProcess::finished()

      Purpose:

      Fetching buffered standard output while process is still running in order to provide feedback to the user.

      This setup is represented by the following code (The complete QtCreator project is attached to this report, including the batch file).

      StdReader::StdReader(QProcess *process, QObject *parent)
      : QObject(parent)
      , _process(process)
      {
          // if you disconnect this signal all buffered output will be printed after the process has finished
          connect(_process, SIGNAL(readyReadStandardOutput()), this, SLOT(readOutput()));
      
          connect(_process, SIGNAL(finished(int,QProcess::ExitStatus)), SLOT(finished()));
      }
      
      void StdReader::readOutput()
      {
          foreach (const QString &text, QString(_process->readAllStandardOutput()).split(QRegExp("[\n\r]"), QString::SkipEmptyParts))
          {
              qDebug() << text;
          }
      }
      
      void StdReader::finished()
      {
          qDebug() << "============ finished ============";
      
          foreach (const QString &text, QString(_process->readAllStandardOutput()).split(QRegExp("[\n\r]"), QString::SkipEmptyParts))
          {
              qDebug() << text;
          }
      }
      

      Scenario 1: The code as shown above is used and leads to the following observation

      1. The first part of the generated numbers are printed out during StdReader::readOutput() (as expected).
      2. The process finishes, StdReader::finished() is called in order to fetch the last part of the buffered output. As a matter of fact the remaining part of the generated number list ist not returned by QProcess::readAllStandardOutput() (not expected), calling this function just returns the empty string (buffer seems to be empty?, output is lost?). According to the documenation of QProcess::finished() all buffers remain intact after the process has finished, so fetching the buffered output this way should be no problem.

      Scenario 2: In the above code QProcess::readyReadStandardOutput() is disconnected from it's slot (for example by removing this line).

      1. The process runs to completion without simultaneous output (as expected).
      2. The process finishes, StdReader::finished() is called and the complete buffered output is printed with all generated numbers (as expected).

      Conclusion:

      As far as I see it scenario 1 should be the correct way of fetching the buffered output online while the process is still running. Scenario 2 can be considered as a workaround but it delivers all standard output not until the process has finished.

      Please note that this is a regression bug since this code is working with Qt 4.8.4!

      Attachments

        Activity

          People

            jbornema Joerg Bornemann
            derhandwerk Wolf Bublitz
            Votes:
            1 Vote for this issue
            Watchers:
            5 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: