PDA

View Full Version : QProcess wo event loop



brazso
11th February 2009, 12:12
I wish to start an external process inside my gui application (QT v4.4.2, Win), the application can be suspended until the termination of that process. So far I used successfully the following code snippet:


QProcess process;
process.start("something", args);

if (!process.waitForStarted()){
// some error msg dialog
return;
}

if (!process.waitForFinished(-1))
return;

Then I could read the the complete output/error channel of the terminated process. However I wish I could process the output channels line by line during the operation of the process, because I want to feed a progress bar. Therefore I replaced the waitForFinished block to something like that:


while (process.state()==QProcess::Running /* || process.waitForReadyRead(1) */){
// reading process output channels
}

My problem is that this while loop never ends, the process state always remains in Running state. How can I achieve my aim without asynchronous usage?

boudie
11th February 2009, 22:07
Have you tried a connection to this signal:
QProcess::readyReadStandardOutput ()

brazso
9th March 2009, 09:57
Thanks for your reply but I wish to avoid the asynchronous process calling due to the nature of my task. Finally I think I have found the solution, process.waitForReadyRead must be used:


process.start("something", args);

if (!process.waitForStarted()){
// some error message
return;
}

while (process.waitForReadyRead(-1)){
QByteArray newData = process.readAllStandardOutput();
result = QString::fromLocal8Bit(newData);
qDebug(qPrintable(QString("waitForReadyRead:")+result));
}

// here the process is already finished

And I do not need the process.waitForFinished method at all.