Skip to content
Advertisement

I cannot get stdout to update in Qt

I need to call an executable, from my QT application, monitor the output and then display the out put in a text browser.

The executable I call from my QT app will run for many hours (~12) and each time the executable spits something out to stdout, I need to parse the output to be able to monitor its progress, then display it all to the screen.

I did this below to start… which is a start… But it only displays the output to the window when the executable finishes, and I need it to update in real time… as well as parsing the output

Put new code in… Program runs, but nothing is ported to the screen, maybe I need to set the process to write to stdout? It will display every output when program is canceled or finishes… The whole function acts that way? Surely I am doing something silly…

btw the executable I am calling does not buffer output… when called from the command line it outputs as the program runs… Runs all except the finished SIGNAL bit…

New Code: (EDIT… Solution!)

 monit::monit(QWidget *parent) :
QDialog(parent),
ui(new Ui::monit){

 ui->setupUi(this);


r = new QProcess(this);

connect(r, SIGNAL(readyReadStandardError()), this, SLOT(updateError()));
connect(r, SIGNAL(readyReadStandardOutput()), this, SLOT(updateText()));
//connect(r, SIGNAL(started()), this, SLOT(updateExit()));

}

ProcChainMonitor::~ProcChainMonitor()
{
    delete ui;
}

void ProcChainMonitor::on_buttonStart_clicked()
{
    QString program = "prog";
    QStringList arguments;
    arguments << "arg";

r->setWorkingDirectory("dir");
r->start(program, arguments);
}

void ProcChainMonitor::updateError()
{

QByteArray data = r->readAllStandardError();
ui->textEdit->append(data);

if (r->exitStatus()!= NULL){
    ui->textEdit->append("Exited");
    ui->textEdit->append(QString::number(r->exitCode()));
}
}

void ProcChainMonitor::updateText()

{

QByteArray data = r->readAll();
ui->textEdit->append(data);

if (r->exitStatus()!= NULL){
    ui->textEdit->append("Exited");
    ui->textEdit->append(QString::number(r->exitCode()));
}

}

Advertisement

Answer

Look at https://stackoverflow.com/questions/1000674/turn-off-buffering-in-pipe which will remove the buffering and cause you to get input almost immediately (YMMV, I haven’t tested this)

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement