What would be the preferred way in Qt of reading a child process output one line at a time as it appears?
I tried connecting QProcess signal readyReadStandardOutput to the function that calls QProcess method readLine.
The preferred way is the asynchronous way, using signals emitted by QIODevice
. Your approach is correct. Make sure that you read all available lines within your slot:
process->setReadChannel(QProcess::StandardOutput);
while (process->canReadLine()) {
QString line = QString::fromLocal8bit(process->readLine());
...
}
Also remember that once you read something, it's not available to be read again. QIODevice
's signals need to be used with care - you can't connect an arbitrary number of consumers to the readyRead
signal and perform the reading in each of them. It won't work the way you might have expected it to. If the first reader reads all of the data, the subsequent ones won't be able to read it again.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With