Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QProcess::readAllStandardOutput gives flaky outputs
Qt 6.11 is out! See what's new in the release blog

QProcess::readAllStandardOutput gives flaky outputs

Scheduled Pinned Locked Moved General and Desktop
3 Posts 3 Posters 2.9k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N Offline
    N Offline
    noob11
    wrote on last edited by
    #1

    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.

    1 Reply Last reply
    0
    • JeroentjehomeJ Offline
      JeroentjehomeJ Offline
      Jeroentjehome
      wrote on last edited by
      #2

      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

      Greetz, Jeroen

      1 Reply Last reply
      0
      • W Offline
        W Offline
        walteste
        wrote on last edited by
        #3

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

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved