Home > OS >  How to get real time std out from python script while using QProcess
How to get real time std out from python script while using QProcess

Time:08-15

here my code, real time output i can get only when using ping {address} in QLineEdit as cmd input. i want to same effect, but when execute python test.py.

QT C code:

#include "dialog.h"
#include "ui_dialog.h"
#include <QProcess>

QProcess proc;

Dialog::Dialog(QWidget *parent)
    : QDialog(parent)
    , ui(new Ui::Dialog)
{
    ui->setupUi(this);

    connect(ui->btnStart, SIGNAL(clicked()), this, SLOT(startProcess()));
    connect(ui->btnStop, SIGNAL(clicked()), this, SLOT(stopProcess()));
}

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

void Dialog::startProcess()
{
    QString str1 = ui->textCmd->text(); // получаем строку из первого QLineEdit
    connect(&proc, &QProcess::readyReadStandardOutput, this, &Dialog::slotDataOnStdout);
    proc.start(str1);
}

void Dialog::stopProcess()
{
    proc.close();
}

void Dialog::slotDataOnStdout()
{
    QString output = proc.readAllStandardOutput().toStdString().c_str();
    ui->outputCmd->append(output);
}

void Dialog::on_btnClean_clicked()
{
    ui->outputCmd->clear();
}

Python test.py code

import time

while True:
    print("test output")
    time.sleep(1)

please help... maybe it's problem with executing python script? or with signal and slot connection in void Dialog::slotDataOnStdout()

ui image, if you really need this app ui

CodePudding user response:

fixed just by add -u flag for execute command.

example:

python3 -u test.py

problem was with buffering.

  • Related