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

QProcess : error and state doesn't change

    XMLWordPrintable

    Details

    • Type: Bug
    • Status: Closed
    • Priority: Not Evaluated
    • Resolution: Invalid
    • Affects Version/s: 5.12.1
    • Fix Version/s: None
    • Labels:
      None
    • Environment:
      Ubuntu 18.04
      Qt 5.12.1 (installed from offline installer)
      Qt Creator 4.8.1
    • Platform/s:
      Linux/X11

      Description

      startDetached return true but error and state doesn't change. WaitForStarted and WaitForFinished are always false without errors. Process is running while its state is NotRunning.

      Here is the core code. You can check attachment for a "ready to compile" project.

      #include "PythonDetector.h"
      #include <QProcess>
      #include <iostream>
      #include <QApplication>
      #include <QTest>PythonDetector::PythonDetector(QObject *parent) : QObject(parent)
      {}void PythonDetector::detect()
      {
          bool state;
          QString path = "";
          QString  command("python3");
          QStringList params;    command = "time";
          params << "sleep" << "1";
          QProcess *process = new QProcess();
          connect(process, SIGNAL(errorOccurred(QProcess::ProcessError)), this, SLOT(errorOccurred(QProcess::ProcessError)));
          connect(process, SIGNAL(stateChanged(QProcess::ProcessState)), this, SLOT(stateChanged(QProcess::ProcessState)));
          std::cout << "Starting. Last Error=" << process->error()<< ", current State= " << process->state() << std::endl;
          state=process->startDetached(command, params, path);
          std::cout << "Waiting for start. StartDetached state=" << state <<" => Last Error=" << process->error()<< ", current State= " << process->state() << std::endl;
          qApp->processEvents();
          state = process->waitForStarted(-1);
          qApp->processEvents();
          std::cout << "Waiting for finish. Started state=" << state <<" => Last Error=" << process->error()<< ", current State= " << process->state() << std::endl;
          while(!process->waitForFinished(-1)){
              std::cout << "Waiting for finish. Finished state=" << state <<" => Last Error=" << process->error()<< ", current State= " << process->state() << std::endl;
              qApp->processEvents();
              QTest::qWait(1000);
          }
          //process->close();
          std::cout << "End of process" << std::endl;}
      void PythonDetector::errorOccurred(QProcess::ProcessError error)
      {
          std::cout << "Error : " << error << std::endl;
      }
      void PythonDetector::stateChanged(QProcess::ProcessState newState)
      {
          std::cout << "State : " << newState << std::endl;
      }
      

      The object is used with a

      MainWindow::MainWindow(){
      ..
      QTimer::singleShot(100,this, SLOT(test()));
      ..
      }
      
      void MainWindow::test()
      {
          PythonDetector detector;
          detector.detect();
      }
      

      Output:

      Starting. Last Error=5, current State= 0
      Waiting for start. StartDetached state=1 => Last Error=5, current State= 0
      Waiting for finish. Started state=0 => Last Error=5, current State= 0
      Waiting for finish. Finished state=0 => Last Error=5, current State= 0
      Waiting for finish. Finished state=0 => Last Error=5, current State= 0
      0.00user 0.00system 0:01.00elapsed 0%CPU (0avgtext+0avgdata 2132maxresident)k
      0inputs+0outputs (0major+77minor)pagefaults 0swaps
      Waiting for finish. Finished state=0 => Last Error=5, current State= 0
      Waiting for finish. Finished state=0 => Last Error=5, current State= 0
      ...
      

      End of process is never reached as waitForFinished is always false.
      I added "qApp->processEvents();" just to be sure that events are processed.

        Attachments

        1. test.zip
          6 kB
          Julien W
        No reviews matched the request. Check your Options in the drop-down menu of this sections header.

          Activity

            People

            • Assignee:
              thiago Thiago Macieira
              Reporter:
              julienw Julien W
            • Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved:

                Gerrit Reviews

                There are no open Gerrit changes