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. Real time display of QProcess output in a textBrowser
Forum Updated to NodeBB v4.3 + New Features

Real time display of QProcess output in a textBrowser

Scheduled Pinned Locked Moved General and Desktop
6 Posts 5 Posters 5.5k 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.
  • T Offline
    T Offline
    tanny007
    wrote on 11 Apr 2012, 01:36 last edited by
    #1

    Hello,
    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);}@

    1 Reply Last reply
    0
    • A Offline
      A Offline
      asergiu
      wrote on 11 Apr 2012, 02:50 last edited by
      #2

      I guess right now your process exit code is not 0 or "success". You should probably run echo within a command processor or shell on unix

      1 Reply Last reply
      0
      • S Offline
        S Offline
        sidewinder
        wrote on 11 Apr 2012, 06:36 last edited by
        #3

        Most likely there is nothing to read yet when you call readAllStandardOutput().
        You can block until there is something to read by calling waitForReadyRead() or you may connect readReady signal to your slot in which you can read available data.

        "Never memorize what you can look up in books."
        Albert Einstein

        1 Reply Last reply
        0
        • A Offline
          A Offline
          andre
          wrote on 11 Apr 2012, 06:45 last edited by
          #4

          Please use the latter approach. Blocking is in almost all cases a Bad Idea(TM)

          A few more remarks:

          Please read QProcess documentation on how to pass parameters to the applications. It does not work the way you are trying now

          Please be aware that some applications behave differently if they run in a terminal than if they are run in another way, in terms of output buffering in the other application. Search for that to find more details. It can mean that you might not get a continious stream of data, but rather discrete blocks of lots of output in one go.

          There is little chance there already is data on your process object at the moment you call readAllStandardOutput(). The other process needs time to start, and in the meantime your application is already continuing. So, by the time echo actually does something, your routine has already run. Worse: p will likely have been destroyed already, when it goes out of scope.

          1 Reply Last reply
          0
          • G Offline
            G Offline
            giesbert
            wrote on 11 Apr 2012, 06:54 last edited by
            #5

            Try to wait for the process to start:

            @
            QProcess p;
            p.start("echo hye");
            p.waitForStarted(); // <-- new line
            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);
            }
            @

            Nokia Certified Qt Specialist.
            Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

            1 Reply Last reply
            0
            • T Offline
              T Offline
              tanny007
              wrote on 11 Apr 2012, 14:34 last edited by
              #6

              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();@

              1 Reply Last reply
              0

              1/6

              11 Apr 2012, 01:36

              • Login

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