title: Communicating with QProcess via stdin/stdout and QDataStream
body: <p>I'm trying to write an app that would spawn a subprocess and communicate with it via standard output/input. Just to get the hang of it I tried writing a simple app that would send a message to the subprocess, and the subprocess would receive it and send it back.  After frankly ridiculous amount of trial and error I managed to get a message to the subprocess, but I can't figure out how to send it back.</p>
  
  <p>Here's my attempt:</p>
  
  <pre><code>#include &lt;QApplication&gt;
  #include &lt;QDataStream&gt;
  #include &lt;QFile&gt;
  #include &lt;QDebug&gt;
  #include &lt;QProcess&gt;
  #include &lt;QThread&gt;
  
  #define dumpval(x) qDebug()&lt;&lt;#x&lt;&lt;'='&lt;&lt;x
  
  void slave()
  {
      QApplication::setApplicationName("slave");
      qSetMessagePattern("%{appname}: %{message}");
      qDebug()&lt;&lt;"started";
      QFile input;
      QFile output;
      dumpval(input.open(stdin, QFile::ReadOnly));
      dumpval(output.open(stdout, QFile::WriteOnly));
  
      QObject::connect(&amp;output, &amp;QIODevice::bytesWritten, [](int bytesWritten){dumpval(bytesWritten);});
  
      QDataStream inputStream(&amp;input);
      QDataStream outputStream(&amp;output);
  
      QByteArray data;
      while (true){
          inputStream&gt;&gt;data;
          dumpval(data);
          if (!data.isEmpty()) break;
          inputStream.resetStatus();
          QThread::sleep(1);
      }
  
      dumpval(output.isWritable());
      outputStream&lt;&lt;data;
      dumpval(output.waitForBytesWritten(-1));
  
      qDebug()&lt;&lt;"data written";
      qDebug()&lt;&lt;"stopped";
  }
  
  void master(QString path)
  {
      QApplication::setApplicationName("master");
      qSetMessagePattern("%{appname}: %{message}");
      qDebug()&lt;&lt;"started";
      QProcess p;
      QObject::connect(&amp;p, &amp;QIODevice::bytesWritten, [](int bytesWritten){dumpval(bytesWritten);});
      p.setProgram(path);
      p.setArguments({"slave"});
      p.setProcessChannelMode(QProcess::ForwardedErrorChannel);
      p.start();
      p.waitForStarted();
  
      QDataStream stream(&amp;p);
      QByteArray data = "this is a test";
      stream&lt;&lt;data;
      dumpval(p.waitForBytesWritten(-1));
  
      data.clear();
  
      while (true){
          stream&gt;&gt;data;
          dumpval(data);
          if (!data.isEmpty()) break;
          stream.resetStatus();
          QThread::sleep(1);
      }
  
      qDebug()&lt;&lt;"stopped";
  }
  
  int main(int argc, char** argv)
  {
      if (argc == 1) master(argv[0]);
      else slave();
  }
  </code></pre>
  
  <p>And here's the output of this code:</p>
  
  <pre><code>master: started
  master: bytesWritten = 18
  master: p.waitForBytesWritten(-1) = true
  master: data = ""
  slave: started
  slave: input.open(stdin, QFile::ReadOnly) = true
  slave: output.open(stdout, QFile::WriteOnly) = true
  slave: data = "this is a test"
  slave: output.isWritable() = true
  slave: output.waitForBytesWritten(-1) = false
  slave: data written
  slave: stopped
  master: data = ""
  master: data = ""
  master: data = ""
  master: data = ""
  master: data = ""
  master: data = ""
  ^C
  </code></pre>
  
  <p>What am I doing wrong?</p>