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. How to display the content which received from other application line by line.

How to display the content which received from other application line by line.

Scheduled Pinned Locked Moved Solved General and Desktop
6 Posts 4 Posters 588 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
    Trojan Arun
    wrote on last edited by
    #1

    Here the below example:
    1.Text browser i used to print the string which received form tclsh.exe application
    2.I m not able to display the string line by line into text browser, instead at one shot all the string are appended in Text browser widget.

    eg:
    QProcess *myProcess = new QProcess();
    arguments << "C:/gui_run.tcl";
    myProcess->start("C:/init_config/tclsh.exe",arguments);
    myProcess->waitForFinished(3000);
    while (myProcess->canReadLine()) {
    qDebug("inside while");
    rets = myProcess->readLine();
    myProcess->waitForFinished(3000);
    qDebug("%s", qUtf8Printable(rets));
    ui->textBrowser->append(rets);
    qDebug("after console print");
    }
    But qDebug is able to read and display the content line by line but text browser isnt!

    1 Reply Last reply
    0
    • AnatolySA Offline
      AnatolySA Offline
      AnatolyS
      wrote on last edited by
      #2

      Call https://doc.qt.io/qt-5/qcoreapplication.html#processEvents after each append call.

      1 Reply Last reply
      3
      • mrjjM Offline
        mrjjM Offline
        mrjj
        Lifetime Qt Champion
        wrote on last edited by
        #3

        Hi
        You are using waitForFinished which is a blocking call so
        you are preventing the event loop to run and hence the textBrowser does not
        have time to update since you also loop with while.

        A fix as @AnatolyS says is to use processEvent to force it to run.
        This is not considered the best practice and can have side effects but often
        it just works as intended if no extra local event loops are used etc.

        The alternative is to use the signals of QProcess and do it all in
        a non blocking asynchronously way.

        aha_1980A T 2 Replies Last reply
        4
        • mrjjM mrjj

          Hi
          You are using waitForFinished which is a blocking call so
          you are preventing the event loop to run and hence the textBrowser does not
          have time to update since you also loop with while.

          A fix as @AnatolyS says is to use processEvent to force it to run.
          This is not considered the best practice and can have side effects but often
          it just works as intended if no extra local event loops are used etc.

          The alternative is to use the signals of QProcess and do it all in
          a non blocking asynchronously way.

          aha_1980A Offline
          aha_1980A Offline
          aha_1980
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @mrjj said in How to display the content which received from other application line by line.:

          The alternative is to use the signals of QProcess and do it all in
          a non blocking asynchronously way.

          I'd say that is the only correct solution.

          Qt has to stay free or it will die.

          1 Reply Last reply
          3
          • mrjjM mrjj

            Hi
            You are using waitForFinished which is a blocking call so
            you are preventing the event loop to run and hence the textBrowser does not
            have time to update since you also loop with while.

            A fix as @AnatolyS says is to use processEvent to force it to run.
            This is not considered the best practice and can have side effects but often
            it just works as intended if no extra local event loops are used etc.

            The alternative is to use the signals of QProcess and do it all in
            a non blocking asynchronously way.

            T Offline
            T Offline
            Trojan Arun
            wrote on last edited by
            #5

            @mrjj How you will call waitforfinished in signals? is there any other process call to help out this issue.

            mrjjM 1 Reply Last reply
            0
            • T Trojan Arun

              @mrjj How you will call waitforfinished in signals? is there any other process call to help out this issue.

              mrjjM Offline
              mrjjM Offline
              mrjj
              Lifetime Qt Champion
              wrote on last edited by
              #6

              @Trojan-Arun
              Hi
              You just hook up to the signals and Qt will call you when data comes in or error happens.

              void MainWindow::on_pushButton_released()
              {
              
                  QProcess *process = new QProcess(this);
                  connect(process, &QProcess::started, []() {
                      qDebug() << "Started!";
                  });
                  connect(process, &QProcess::readyRead, [this, process]() {
                      QTextStream outputStream(process->readAllStandardOutput());      
                      ui->textBrowser->append(outputStream.readAll());
                  });
                  connect(process, &QProcess::readyReadStandardOutput, [this, process]() {
                      QTextStream outputStream(process->readAllStandardOutput());
                      ui->textBrowser->append(outputStream.readAll());
                  });
                  connect(process, &QProcess::readyReadStandardError, [this, process]() {
                      QTextStream errorStream(process->readAllStandardError());
                      ui->textBrowser->append(errorStream.readAll());
                  });
                  connect(process, qOverload<int, QProcess::ExitStatus>(&QProcess::finished), [this,
                                                                                               process](int exitCode,
                  QProcess::ExitStatus exitStatus) {
                      ui->textBrowser->append(QString::number(exitCode) + " " + QString::number(exitStatus));
                      process->deleteLater();
                  });
              
                  process->start("cmd.exe", QStringList()  << "/K cd %USERPROFILE%");
              }
              
              

              The sample adds the output to a textbrowser.

              You can add your code to readyRead slot and use readline there if needed.

              1 Reply Last reply
              3

              • Login

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