c++ - Qt 5 and QProcess redirect stdout with signal/slot readyRead -


this problem bothering me because should work, sadly not. try achieve read standard output of process , make process handle i.e. print out.

the process produces output looks this:

#include <stdio.h> #include <stdlib.h> #include <iostream>  int main() {     (int = 0; < 100; i++) {         printf("yes %d\n",i);         fflush(stdout);         sleep(1);     }     return 0; } 

the process started in application this:

#include <qprocess> ... qprocess * process = new qprocess; someclass * someclass = new someclass(process); connect(process,signal(readyread()),someclass,slot(onreadyread()));  process->start("../test/test",qstringlist()); if (!process->waitforstarted(4000)) {     qdebug() << "process did not start."; } ... void someclass::onreadyread() {     qdebug() << "reading:" << process->readallstdoutput(); } 

my expected output be:

reading: yes 0 reading: yes 1 ... reading: yes 99 

however no output @ all. , when use qcoreapplication output not through signal/slot directly in console.

i dont understand because works in application uses qt 4.8.

my question is, experiencing same problem or know how can expected behaviour?

your problem in answer provide lies in misunderstanding how reading works. returns whatever data you've got there, whether there line endings or not. spawning thread , sleeping between lines, you're sending inter-process data in line-sized chunks since pipe flushed when wait long enough.

so, answer, while working, not how 1 should it. need use readline() chop incoming data lines. below example following qualities:

  1. there's 1 executable :)
  2. only qt apis used. reduces runtime memory consumption.
  3. both processes cleanly terminate.
  4. the amount of code minimal practicable.

// https://github.com/kubao/stackoverflown/tree/master/questions/process-17856897 #include <qtcore>  qtextstream out{stdout};  class slave : public qobject {     qbasictimer m_timer;     int m_iter = 0;     void timerevent(qtimerevent * ev) override {         if (ev->timerid() == m_timer.timerid()) {             out << "iteration " << m_iter++ << endl;             if (m_iter > 35) qapp->quit();         }     } public:     slave(qobject *parent = nullptr) : qobject(parent) {         m_timer.start(100, this);     } };  class master : public qobject {     q_object     qprocess m_proc{this};     q_slot void read() {         while (m_proc.canreadline()) {             out << "read: " << m_proc.readline();             out.flush(); // endl implicitly flushes, must same         }     }     q_slot void started() {         out << "started" << endl;     }     q_slot void finished() {         out << "finished" << endl;         qapp->quit();     } public:     master(qobject *parent = nullptr) : qobject(parent) {         connect(&m_proc, signal(readyread()), slot(read()));         connect(&m_proc, signal(started()), slot(started()));         connect(&m_proc, signal(finished(int)), slot(finished()));         m_proc.start(qapp->applicationfilepath(), {"dummy"});     } };  int main(int argc, char *argv[]) {     qcoreapplication app{argc, argv};     if (app.arguments().length() > 1)         new slave{&app}; // called argument, slave process     else         new master{&app}; // no arguments, master     return app.exec(); }  #include "main.moc" 

Comments

Popular posts from this blog

curl - PHP fsockopen help required -

HTTP/1.0 407 Proxy Authentication Required PHP -

c# - Resource not found error -