Real time display of QProcess output in a textBrowser
-
Hello,
I am a newbie in qt development and i want to transfer the output of QProcess to a textBrowser in real time. I started by executing a simple echo command,but the output of the program is not getting displayed.
What am i doing wrong????@QProcess p;
p.start("echo hye");
QByteArray byteArray = p.readAllStandardOutput();
QStringList strLines = QString(byteArray).split("\n");
QString line= p.readAllStandardOutput();
if(p.state()==QProcess::NotRunning)
ui->textBrowser->append("not running");
foreach (QString line, strLines){
ui->textBrowser->append(line);}@ -
I guess right now your process exit code is not 0 or "success". You should probably run echo within a command processor or shell on unix
-
Most likely there is nothing to read yet when you call readAllStandardOutput().
You can block until there is something to read by calling waitForReadyRead() or you may connect readReady signal to your slot in which you can read available data. -
Please use the latter approach. Blocking is in almost all cases a Bad Idea(TM)
A few more remarks:
Please read QProcess documentation on how to pass parameters to the applications. It does not work the way you are trying now
Please be aware that some applications behave differently if they run in a terminal than if they are run in another way, in terms of output buffering in the other application. Search for that to find more details. It can mean that you might not get a continious stream of data, but rather discrete blocks of lots of output in one go.
There is little chance there already is data on your process object at the moment you call readAllStandardOutput(). The other process needs time to start, and in the meantime your application is already continuing. So, by the time echo actually does something, your routine has already run. Worse: p will likely have been destroyed already, when it goes out of scope.
-
Try to wait for the process to start:
@
QProcess p;
p.start("echo hye");
p.waitForStarted(); // <-- new line
QByteArray byteArray = p.readAllStandardOutput();
QStringList strLines = QString(byteArray).split("\n");
QString line= p.readAllStandardOutput();
if(p.state()==QProcess::NotRunning)
ui->textBrowser->append("not running");
foreach (QString line, strLines)
{
ui->textBrowser->append(line);
}
@ -
I am still not able to get the output in a textBrowser .
I changed the Qprocess parameters and tried both waitForStarted() and waitForReadyRead() so that the process starts in time and the results are available.
I added waitForFinished() so that the process does not terminate when it goes out of scope.
@QProcess p;
p.start("echo", QStringList() << "hye");
p.waitForStarted();
QByteArray byteArray = p.readAllStandardOutput();
QStringList strLines = QString(byteArray).split("\n");
QString line= p.readAllStandardOutput();
if(p.state()==QProcess::NotRunning)
ui->textBrowser->append("not running");
ui->textBrowser->append(line);
p.waitForFinished();@