QProcess::readAllStandardOutput gives flaky outputs



  • Hi all,

    I am trying to create a GUI in Qt4 for my tcl based tool. In order to populate widgets I need to execute some tcl commands. I read about QProcess and I am invoking tcl scripts using QProcess and then grabbing their output from stdout. Suppose I execute 3 commands in tcl then when I query stdout I believe I should see 3 outputs corresponding to each of the three commands, however this is not happening consistently. Behavior is flaky.

    As you can see in the main.cpp I am executing multiple commands using runTclCommand() function and in the end executing getData() function to read stdout.

    main.cpp:
    @#include <QApplication>
    #include <QProcess>
    #include <QDebug>
    #include "Tclsh.h"

    int main(int argc, char *argv[])
    {
    QApplication a(argc, argv);
    QByteArray out;
    Tclsh *tcl = new Tclsh;
    tcl->startTclsh();
    tcl->runTclCommand("set ::tcl_interactive 1\n");
    tcl->runTclCommand("set a 23\n");
    tcl->runTclCommand("puts $a\n");
    tcl->runTclCommand("set a 40\n");
    tcl->runTclCommand("puts $a\n");
    out = tcl->getData();
    }@

    Tclsh.cpp
    @#include <QProcessEnvironment>
    #include <QProcess>
    #include <QDebug>
    #include "Tclsh.h"

    void Tclsh::startTclsh() {
    QString program = "/usr/bin/tclsh8.4";
    this->setProcessChannelMode(QProcess::MergedChannels);
    this->start(program);
    if ( !this->waitForStarted()) {
    qDebug()<<"ERROR Starting tclsh";
    }
    return;
    }

    void Tclsh::runTclCommand(const char *cmd) {
    qDebug()<<"CMD:"<<cmd;
    this->write(cmd);
    if (!this->waitForBytesWritten()) {
    qDebug()<<"Error in writing data";
    }
    }

    QByteArray Tclsh::getData() {
    if (!this->waitForReadyRead()) {
    qDebug()<<"Error in reading stdout: Ready read signal is not emitted";
    }
    QByteArray data = this->readAllStandardOutput();
    qDebug()<<"DATA:"<<data;
    return data;
    }@

    However, sometime I get the following output:
    @CMD: set ::tcl_interactive 1

    CMD: set a 23

    CMD: puts $a

    CMD: set a 40

    CMD: puts $a

    DATA: "1
    % 23
    % 23
    % "@

    And sometimes this:
    @CMD: set ::tcl_interactive 1

    CMD: set a 23

    CMD: puts $a

    CMD: set a 40

    CMD: puts $a

    DATA: "1
    " @

    I do not understand why this is happening. I would really appreciate if someone can point me to the error in my approach here.

    Thanks for your time.



  • Hi there, the waitforready read has a timeout. If you don't want to use it, set to -1. Couldn't find the default in de libs, so best set it yourself. You should take into account that the GUI will freeze if in the same thread when waiting for data.
    greetz



  • Yes, the best would be to have this in a separate thread.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.