Real time display of QProcess output in a textBrowser

11,997

to read standard output you need to either call waitForReadyRead() before reading stardard output , or you need to connect Qprocess's signal readyReadStandardOutput() to your slot and read standard output from slot.

also make sure that your QProcess is not on stack.

I tried following code works fine.

EDIT:

MyProcess::MyProcess(QObject *parent) :
    QObject(parent)
{
    QString program = "echo";
    QStringList arguments;
    arguments << "Hello";
    mProcess.start(program,arguments);
    connect(&mProcess,SIGNAL(readyReadStandardOutput()),this,SLOT(readyReadStandardOutput()));
    connect(&mProcess,SIGNAL(readyReadStandardError()),this,SLOT(readyReadStandardError()));
}

void MyProcess::readyReadStandardOutput(){
    qDebug()<< mProcess.readAllStandardOutput();
}

void MyProcess::readyReadStandardError(){
    qDebug() << mProcess.readAllStandardError();
}
Share:
11,997
Tanmay J Shetty
Author by

Tanmay J Shetty

Updated on July 07, 2022

Comments

  • Tanmay J Shetty
    Tanmay J Shetty almost 2 years

    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);}
    

    P.S. I am on a linux machine.

    EDIT: 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();
    
  • Tanmay J Shetty
    Tanmay J Shetty about 12 years
    I would like to display the results in a textBrowser,but this code displays results in qt's terminal itself.
  • Kunal
    Kunal about 12 years
    well, you can use ui->textBrowser->append instead of qDebug()
  • Answeror
    Answeror almost 11 years
    See sites.google.com/site/marcsturmspages/qt/… for the buffering issue and its workaround.
  • Valentin H
    Valentin H almost 9 years
    Thanks for reminding on waitForReadyRead()! For my app it was sufficient and using this doesn't require to subclass from QProcess.