Starting QProcess and reading its output one line at a time
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.