title: reading and writing to QProcess in Qt Console Application
body: <p><em>Noted: this appears to be a specific issue question but hopefully it can be edited for all to related to</em></p>
  
  <p>I need to interact with a QProcess object.</p>
  
  <p><strong>The Problem:</strong></p>
  
  <p>I am not getting any output from <code>QProcess</code> after calling <code>QProcess:write(input)</code></p>
  
  <p><strong>More Info:</strong></p>
  
  <p>Going through the <a href="http://doc.qt.io/qt-5/qprocess.html" rel="nofollow noreferrer">doc pages</a> led me to create an example below:</p>
  
  <p>I have a script requesting user input, and finally displaying and appropriate message based on the user input.</p>
  
  <p><strong>Testing:</strong></p>
  
  <p>After adding a "log" feature to my script for testing, the following occurs:</p>
  
  <ul>
  <li>script executes</li>
  <li>script requests user input (confirmed by the 'first' <code>qDebug() &lt;&lt; p-&gt;readAll()</code>)</li>
  <li>script accepts input from <code>QProcess</code> (confirmed by script 'log output')</li>
  </ul>
  
  <p>After this, no output is received. The following 2 debug statements both fire (i.e. wait 30s each)</p>
  
  <pre><code>if (!p-&gt;waitForReadyRead()) {
      qDebug() &lt;&lt; "waitForReadyRead() [false] : CODE: " &lt;&lt; QVariant(p-&gt;error()).toString() &lt;&lt; " | ERROR STRING: " &lt;&lt; p-&gt;errorString();
  }
  if (!p-&gt;waitForFinished()) {
      qDebug() &lt;&lt; "waitForFinished() [false] : CODE: " &lt;&lt; QVariant(p-&gt;error()).toString() &lt;&lt; " | ERROR STRING: " &lt;&lt; p-&gt;errorString();
  }
  </code></pre>
  
  <p>Followed by:</p>
  
  <pre><code>QString s = QString(p-&gt;readAll() + p-&gt;readAllStandardOutput());
  </code></pre>
  
  <p>where <code>s</code> is an empty string.</p>
  
  <p>The issue is <code>s</code> <em>should</em> contain either "success" or "failed"</p>
  
  <p><strong>Calling Code:</strong></p>
  
  <pre><code>QString cmd = QString("sh -c \"/path/to/bashscript.sh\"");
  QString input = QString("Name");
  QString result = runCommand(cmd, input)
  </code></pre>
  
  <p><strong>Process Code:</strong></p>
  
  <pre><code>//takes 2 parameters, 
  //    cmd which is the code to be executed by the shell
  //    input which acts as the user input
  
  QString runCommand(QString cmd, QString input){
      QProcess *p = new QProcess(new QObject());
      p-&gt;setProcessChannelMode(QProcess::MergedChannels);   //no actual reason to do this
      p-&gt;start(cmd);
      if (p-&gt;waitForStarted()) {
          if (!p-&gt;waitForReadyRead()) {
              qDebug() &lt;&lt; "waitForReadyRead() [false] : CODE: " &lt;&lt; QVariant(p-&gt;error()).toString() &lt;&lt; " | ERROR STRING: " &lt;&lt; p-&gt;errorString();
          }
          if (!p-&gt;waitForFinished()) {
  
              //reads current stdout, this will show the input request from the bash script
              //e.g. please enter your name:
              qDebug() &lt;&lt; p-&gt;readAll();  
  
              //here I write the input (the name) to the process, which is received by the script
              p-&gt;write(ps.toLatin1());
  
              //the script should then display a message i.e. ("success" o "failed")
              if (!p-&gt;waitForReadyRead()) {
                  qDebug() &lt;&lt; "waitForReadyRead() [false] : CODE: " &lt;&lt; QVariant(p-&gt;error()).toString() &lt;&lt; " | ERROR STRING: " &lt;&lt; p-&gt;errorString();
              }
              if (!p-&gt;waitForFinished()) {
                  qDebug() &lt;&lt; "waitForFinished() [false] : CODE: " &lt;&lt; QVariant(p-&gt;error()).toString() &lt;&lt; " | ERROR STRING: " &lt;&lt; p-&gt;errorString();
              }
          }
          QString s = QString(p-&gt;readAll() + p-&gt;readAllStandardOutput());
          return s;
      }
      else{
          qDebug() &lt;&lt; "waitForStarted() [false] : CODE: " &lt;&lt; QVariant(p-&gt;error()).toString() &lt;&lt; " | ERROR STRING: " &lt;&lt; p-&gt;errorString();
      }
      p-&gt;waitForFinished();
      p-&gt;kill();
      return QString();
  }
  </code></pre>
  
  <p><strong>script.sh</strong>  (<code>-rwxrwxr-x</code>)</p>
  
  <pre><code>#!/bin/bash
  #returns 
  #    "success" on non empty $n value
  #    "failed: on empty $n value
  #
  echo "enter your name:"
  read n
  if [[ ! -z $n ]];
  then
          echo "success"
          exit 0;
  else
          echo "failed"
          exit 1;
  fi
  </code></pre>
  
  <p><strong>UPDATE</strong></p>
  
  <p>@KevinKrammer I modified the run command as you said, also using the QStringList with the args.</p>
  
  <p>Still does not get output, infact the <code>waitForReadyRead()</code> and <code>waitForFinished()</code> <code>returns false</code> instantly.</p>
  
  <p>Called with:</p>
  
  <pre><code>QString r = runCommand(QString("text"));
  </code></pre>
  
  <p>Process Code:</p>
  
  <pre><code>QString runCommand(QString input){      
  
      QProcess *p = new QProcess(new QObject());    
      p-&gt;setProcessChannelMode(QProcess::MergedChannels);
  
      //script is the same script refered to earlier, and the `cd /home/dev` IS required
      p-&gt;start("sh", QStringList() &lt;&lt; "-c" &lt;&lt; "cd /home/dev" &lt;&lt; "./script");
      ;
      if (p-&gt;waitForStarted()) {
          if (!p-&gt;waitForReadyRead(5000)) {
              qDebug() &lt;&lt; "waitForReadyRead() [false] : CODE: " &lt;&lt; QVariant(p-&gt;error()).toString() &lt;&lt; " | ERROR STRING: " &lt;&lt; p-&gt;errorString();
          }
          qDebug() &lt;&lt; p-&gt;readAll();
          p-&gt;write(input.toLatin1());
          if(!p-&gt;waitForFinished(5000)){
              qDebug() &lt;&lt; "waitForFinished() [false] : CODE: " &lt;&lt; QVariant(p-&gt;error()).toString() &lt;&lt; " | ERROR STRING: " &lt;&lt; p-&gt;errorString();
          }
          QString s = QString(p-&gt;readAll() + p-&gt;readAllStandardOutput());
          return s;
      }
      else{
          qDebug() &lt;&lt; "waitForStarted() [false] : CODE: " &lt;&lt; QVariant(p-&gt;error()).toString() &lt;&lt; " | ERROR STRING: " &lt;&lt; p-&gt;errorString();
      }
      p-&gt;waitForFinished();
      p-&gt;kill();
      return QString();
  }
  </code></pre>
  
  <p>Terminal Output of the Process:</p>
  
  <pre><code>started
  readChannelFinished
  exit code =  "0"
  waitForReadyRead() [false] : CODE:  "5"  | ERROR STRING:  "Unknown error"
  ""
  waitForFinished() [false] : CODE:  "5"  | ERROR STRING:  "Unknown error"
  Press &lt;RETURN&gt; to close this window...
  </code></pre>
  
  <p>Thoughts on this?</p>
  
  <p><strong>UPDATE 2</strong></p>
  
  <p>@Tarod Thank you for taking the time to make a solution.</p>
  
  <p>It works, however not completely is expected.</p>
  
  <p>I copied over your code, exactly.</p>
  
  <p>Made a few changes in the <code>mReadyReadStandardOutput()</code></p>
  
  <p>See additional info below.</p>
  
  <p><strong>The problem:</strong></p>
  
  <p>After running the application (and script), I get a result -> AWESOME </p>
  
  <p>Everytime it is the incorrect result i.e. "failed". -> NOT AWESOME</p>
  
  <p><strong>Terminal Output:</strong></p>
  
  <pre><code>void MyProcess::myReadyRead()
  void MyProcess::myReadyReadStandardOutput()
  "enter your name:\n"
  ""
  void MyProcess::myReadyRead()
  void MyProcess::myReadyReadStandardOutput()
  "failed\n"
  Press &lt;RETURN&gt; to close this window...
  </code></pre>
  
  <p><strong>script contents:</strong></p>
  
  <pre><code>#!/bin/bash
  echo "enter your name:"
  read n
  echo $n &gt; "/tmp/log_test.txt"
  if [[ ! -z "$n" ]];
  then
          echo "success"
          exit 0;
  else
          echo "failed"
          exit 1;
  fi
  </code></pre>
  
  <p><strong><code>/tmp/log_test.txt</code> output</strong></p>
  
  <pre><code>myname
  </code></pre>
  
  <p><strong>running this manually from console:</strong></p>
  
  <pre><code>dev@dev-W55xEU:~$ ls -la script 
  -rwxrwxr-x 1 dev dev 155 Jan 25 14:53 script*
  
  dev@dev-W55xEU:~$ ./script 
  enter your name:
  TEST_NAME
  success
  
  dev@dev-W55xEU:~$ cat /tmp/log_test.txt 
  TEST_NAME
  </code></pre>
  
  <p>Full code:</p>
  
  <pre><code>#include &lt;QCoreApplication&gt;
  #include &lt;QProcess&gt;
  #include &lt;QDebug&gt;
  
  class MyProcess : public QProcess
  {
      Q_OBJECT
  
  public:
      MyProcess(QObject *parent = 0);
      ~MyProcess() {}
  
  public slots:
      void myReadyRead();
      void myReadyReadStandardOutput();
  };
  
  MyProcess::MyProcess(QObject *parent)
  {
      connect(this,SIGNAL(readyRead()),
              this,SLOT(myReadyRead()));
      connect(this,SIGNAL(readyReadStandardOutput()),
              this,SLOT(myReadyReadStandardOutput()));
  }
  
  void MyProcess::myReadyRead() {
      qDebug() &lt;&lt; Q_FUNC_INFO;
  }
  
  void MyProcess::myReadyReadStandardOutput() {
      qDebug() &lt;&lt; Q_FUNC_INFO;
      // Note we need to add \n (it's like pressing enter key)
      QString s = this-&gt;readAllStandardOutput();
      qDebug() &lt;&lt; s;
      if (s.contains("enter your name")) {
          this-&gt;write(QString("myname" + QString("\n")).toLatin1());
          qDebug() &lt;&lt; this-&gt;readAllStandardOutput();
      }
  }
  
  int main(int argc, char *argv[])
  {
      QCoreApplication a(argc, argv);
  
      MyProcess *myProcess = new MyProcess();
  
      QString program = "/home/dev/script";
  
      myProcess-&gt;start("/bin/sh", QStringList() &lt;&lt; program);
  
      a.exec();
  }
  
  #include "main.moc"
  </code></pre>
  
  <p>script issue? QProcess issue?</p>