title: Proper usage of QProcess
body: <ul>
  <li>Platform: Qt 4.8.2, Win 7</li>
  </ul>
  
  <p>Please consider the following logic flow:</p>
  
  <pre><code>1. App started
  2. functionA() triggered
  3. the app periodically capture some images to external files
  4. end of functionA()
  5. the app create a video from captured images, using ffmpeg as external program
  6. [step 2 -&gt; step 5] may be repeated.
  7. App quit
  </code></pre>
  
  <p>To achieve the flow, I use QProcess to start external program for me to join the images, but I got confused with the proper usage pattern of QProcess. (I don't care about the console messages of ffmpeg, i determine the success of step 5 by checking if the video file is created.)</p>
  
  <p><b>Attempt 1</b></p>
  
  <pre><code>void MyWidget::createAVI()
  {
      checkAndDeleteAVI();
      process = new QProcess(this); // process_ defined as class member;
      process-&gt;start("ffmpeg -f images2 ....");
      process-&gt;waitForFinished(-1);  // (a)
      // (b)
  }
  </code></pre>
  
  <p>At (a), I read the documentation the this call can freeze the main GUI, so should I call from QThread/QRunnable?</p>
  
  <p>At (b), am I missed something here? as when I attempt to close the app, (step 7 in the flow), the app crashes, and I thought that the spawned QProcess is not properly released.</p>
  
  <p><b>Attempt 2</b></p>
  
  <p>I wrote a wrapper class of QProcess as follow:</p>
  
  <p>Launcher.h</p>
  
  <pre><code>class Launcher : public QObject
  {
      Q_OBJECT
  public:
      /** constructor */
      explicit Launcher(QObject *parent = 0);
      /** destructor */
      ~Launcher() {
          if (started_ &amp;&amp; process_-&gt;state() != QProcess::NotRunning)
              process_-&gt;kill();
      } // end_dtor(Launcher)
  Q_SIGNALS:
      void feedbackLog(QString log);
  public Q_SLOTS:
      void launch(QString program, QStringList argList);
  private:
      QProcess * process_;
      bool started_;
  private Q_SLOTS:
      void error(QProcess::ProcessError error);
      void finished(int exitCode, QProcess::ExitStatus status);
      void stateChanged(QProcess::ProcessState state);
  }; // end_class(Launcher)
  
  #endif // LAUNCHER_H
  </code></pre>
  
  <p>Launcher.cpp</p>
  
  <pre><code>#include "launcher.h"
  #include &lt;QCoreApplication&gt;
  #include &lt;QtDebug&gt;
  
  Launcher::Launcher(QObject *parent) : QObject(parent), started_(false)
  {
      process_ = new QProcess(this);
      connect(process_,
              SIGNAL(error(QProcess::ProcessError)),
              SLOT(error(QProcess::ProcessError)));
      connect(process_,
              SIGNAL(finished(int, QProcess::ExitStatus)),
              SLOT(finished(int, QProcess::ExitStatus)));
      connect(process_,
              SIGNAL(stateChanged(QProcess::ProcessState)),
              SLOT(stateChanged(QProcess::ProcessState)));
  } // end_ctor(ExternalLauncher)
  
  void Launcher::launch(QString program, QStringList argList)
  {
      started_ = true;
      process_-&gt;start(program, argList);
      process_-&gt;waitForFinished(-1); // (c)
      Q_EMIT feedbackLog(process_-&gt;readAllStandardOutput());
      process_-&gt;close();
  } // end Launcher::launch()
  
  void Launcher::error(QProcess::ProcessError error)
  {
      /* just feedback some text about the error */
  } // end_slot(Launcher::error)
  
  void Launcher::finished(int exitCode, QProcess::ExitStatus status)
  {
      started_ = false;
      /* feedback some text about finished */
  } // end_slot (Launcher::finished)
  
  void Launcher::stateChanged(QProcess::ProcessState state)
  {
      qDebug() &lt;&lt; "Luancher::stateChanged" &lt;&lt; state;
  }
  </code></pre>
  
  <p>How I use the Launcher:</p>
  
  <pre><code>void MyWidget::createAVI()
  {
      checkAndDeleteAVI();
      launcher_.launch("ffmpeg", "argsList"); // launcher_ defined as class member;
  }
  </code></pre>
  
  <p>So, at (c), is it unnecessary to waitForFinished()? (as I read some info that I should not mix up waitForXXX() and signal/slot framework for QProcess)</p>
  
  <p>Also, is there something I missed for the Launcher class, as I also experience app crash when using this approach.</p>
  
  <p><b>Main question</b>: In general, when to call QProcess::terminate() / QProcess::kill(), and when to delete the QProcess object?</p>
  
  <p>Thanks</p>