Qt: is there a way to send a signal when QProcess writes a string to stdout

I found similar questions, but did not answer for sure. I have a Qt program that launches QProcess and writes the output to the QTextEdit field, as far as it is good. But this only happens when the program has ended. if possible, I would like stdout to print in real time. In an ideal world, there will be some kind of signal that QProcess emits when there is a line ready to read, if this is not possible with QProcess, is it possible at all? Ideally, you can still use the rest of the program while you work.

Here is the code I still have, very simple, it just emits the first line of stdout QProcess in QTextEdit

... extProcess::extProcess(QObject *parent) : QObject(parent) extProcess::extProcess(QObject *parent) : QObject(parent) { proc = new QProcess(this); //initialize proc arguments << "-v"; connect(proc, SIGNAL(readyRead()), this, SLOT(logReady())); } void extProcess::startProcess() { emit clearLog(); emit outLog("--Process started--"); proc->start("/Users/jonathan/Desktop/testgg"); } void extProcess::logReady() { emit outLog(proc->readLine()); } ... 

This is an alternative version that I tried, it will show all the QProcess output, but still only show it when the program ends.

  ... extProcess::extProcess(QObject *parent) : QObject(parent) { proc = new QProcess(this); //initialize proc proc->setProcessChannelMode(QProcess::SeparateChannels); arguments << "-v"; connect(proc, SIGNAL(readyReadStandardOutput()), this, SLOT(logReady())); } void extProcess::logReady() { while(proc->bytesAvailable()){ emit outLog(proc->readLine()); } } void extProcess::startProcess() { emit clearLog(); emit outLog("--Process started--"); proc->start("/Users/jonathan/Desktop/testgg"); } void extProcess::killProcess() { proc->terminate(); emit clearLog(); emit outLog("--Process Terminated--"); } .... 

thanks

+4
source share
3 answers

I am using readAllStandardOutput () for this purpose and it works for me.

However, I noticed that it will not receive any standard output until the process actually flushes its output buffer ("\ n" may not do this automatically, at least not in my platform-specific Windows).

Depending on how the child process writes its output (either a C application or a C ++ application), it needs to call fflush(stdout); or end lines using std::endl; respectively.

+6
source

readLine () probably waits until the first character '\ n' has been read.

readAllStandardOutput , on the other hand, returns all the data available from the standard output of the process.

It seems to me that if you use readAllStandardOutput (), you can get more performance.

You should try, though.

+3
source

In an ideal world, there will be some kind of signal that QProcess emits when there is a line ready to read

Qprocess readyReadStandardOutput() signal is not quite what you are asking for?

You can connect to it, then your slot will receive the available bytesAvailable() data and look for the characters '/n' . Or just readLine() now canReadLine() .

Do not forget to start your process and do not wait for its completion. Also, set ProcessChannelMode to SeparateChannels .

EDIT

I have a class that reads a Qprocess line as follows:

 bool extProcess::logReady() { while( proc->canReadLine()) { QByteArray line = proc->readLine(); /*do some with the line like copying it content to a QTextEdit*/ } } 

And it works pretty well. I'm not sure about the emit outLog() signal! . Are you trying to pass a QByteArray through a signal? The data contained in QByteArray must be stored in a buffer, and you must pass a pointer to this array to anyone connected to it ...

But then again, if you don’t want to use extra memory and are dealing with β€œconsumed data? ...”, do what you need to do with the data in your logReady() method.

+2
source

All Articles