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 capture the output of the Qt program?
Forum Updated to NodeBB v4.3 + New Features

How to capture the output of the Qt program?

Scheduled Pinned Locked Moved Unsolved General and Desktop
5 Posts 4 Posters 653 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.
  • HyEISNH Offline
    HyEISNH Offline
    HyEISN
    wrote on last edited by HyEISN
    #1

    I have two Qt GUI programs, and I'm trying to use QProcess in one program to get the output of the other Qt program, like this:

    QString prog = "myQtProgram.exe";
    QProcess *subProcess = new QProcess;
    connect(subProcess, &QProcess::readyReadStandardOutput, subProcess, [=] {
        QString output = subProcess->readAllStandardOutput();
        qDebug() << output;
    });
    connect(subProcess, &QProcess::readyReadStandardError, subProcess, [=] {
        QString output = subProcess->readAllStandardError();
        qDebug() << output;
    });
    subProcess->start(prog);
    

    But when this code runs, it cannot get any output from another Qt program.
    I found that QtCreator can capture the output of the program even though I run the same program as QtCreator.
    So how does QtCreator do it? Or, what should I do to capture the output of another Qt program?

    Thank you so much!

    R JonBJ 2 Replies Last reply
    0
    • HyEISNH HyEISN

      I have two Qt GUI programs, and I'm trying to use QProcess in one program to get the output of the other Qt program, like this:

      QString prog = "myQtProgram.exe";
      QProcess *subProcess = new QProcess;
      connect(subProcess, &QProcess::readyReadStandardOutput, subProcess, [=] {
          QString output = subProcess->readAllStandardOutput();
          qDebug() << output;
      });
      connect(subProcess, &QProcess::readyReadStandardError, subProcess, [=] {
          QString output = subProcess->readAllStandardError();
          qDebug() << output;
      });
      subProcess->start(prog);
      

      But when this code runs, it cannot get any output from another Qt program.
      I found that QtCreator can capture the output of the program even though I run the same program as QtCreator.
      So how does QtCreator do it? Or, what should I do to capture the output of another Qt program?

      Thank you so much!

      R Offline
      R Offline
      Redman
      wrote on last edited by Redman
      #2

      @HyEISN

      subProcess->setProcessChannelMode(QProcess::ForwardedChannels);
      

      This should redirect the subprocess output to the callers output

      https://doc.qt.io/qt-6/qprocess.html#setProcessChannelMode
      https://doc.qt.io/qt-6/qprocess.html#ProcessChannelMode-enum

      Consider the warning after the enum docs. Could be that you have to use QProcess::SeparateChannels and get the output youself from the subprocess

      HyEISNH 1 Reply Last reply
      0
      • R Redman

        @HyEISN

        subProcess->setProcessChannelMode(QProcess::ForwardedChannels);
        

        This should redirect the subprocess output to the callers output

        https://doc.qt.io/qt-6/qprocess.html#setProcessChannelMode
        https://doc.qt.io/qt-6/qprocess.html#ProcessChannelMode-enum

        Consider the warning after the enum docs. Could be that you have to use QProcess::SeparateChannels and get the output youself from the subprocess

        HyEISNH Offline
        HyEISNH Offline
        HyEISN
        wrote on last edited by
        #3

        @Redman Thank you, I tried it your way, and inspired me to think and try more, but the results ended in failure.
        In fact, I want to get all the output of the Qt program, for example, I deliberately raise an error: Cannot move to target thread (0xe33e8ff8c8), I don't know where this message comes from, but I want to get it.
        I have tried to set WIN32_EXECUTABLE TRUE on Windows to display the console, but this causes the Windows console to read user input at some point, which causes the application to block.

        TomZT 1 Reply Last reply
        0
        • HyEISNH HyEISN

          @Redman Thank you, I tried it your way, and inspired me to think and try more, but the results ended in failure.
          In fact, I want to get all the output of the Qt program, for example, I deliberately raise an error: Cannot move to target thread (0xe33e8ff8c8), I don't know where this message comes from, but I want to get it.
          I have tried to set WIN32_EXECUTABLE TRUE on Windows to display the console, but this causes the Windows console to read user input at some point, which causes the application to block.

          TomZT Offline
          TomZT Offline
          TomZ
          wrote on last edited by
          #4

          @HyEISN sounds like you may want to learn about the difference between stdout and stderr.

          QProcess supports both, and you likely want to combine them into one stream.

          some useful calls:
          myProcess->setProcessChannelMode(QProcess::MergedChannels);
          myProcess->start(QLatin1String("myExe"), arguments, QIODevice::ReadOnly);
          myProcess->waitForFinished(30000);

          1 Reply Last reply
          0
          • HyEISNH HyEISN

            I have two Qt GUI programs, and I'm trying to use QProcess in one program to get the output of the other Qt program, like this:

            QString prog = "myQtProgram.exe";
            QProcess *subProcess = new QProcess;
            connect(subProcess, &QProcess::readyReadStandardOutput, subProcess, [=] {
                QString output = subProcess->readAllStandardOutput();
                qDebug() << output;
            });
            connect(subProcess, &QProcess::readyReadStandardError, subProcess, [=] {
                QString output = subProcess->readAllStandardError();
                qDebug() << output;
            });
            subProcess->start(prog);
            

            But when this code runs, it cannot get any output from another Qt program.
            I found that QtCreator can capture the output of the program even though I run the same program as QtCreator.
            So how does QtCreator do it? Or, what should I do to capture the output of another Qt program?

            Thank you so much!

            JonBJ Offline
            JonBJ Offline
            JonB
            wrote on last edited by
            #5

            @HyEISN
            When you have a GUI program that does not normally produce any output, neither on stdout nor stderr. I am not sure what you are saying you see from where under what circumstances.

            In principle the code you showed is fine. If, say, you make the sub-process program a console application and have it just go like printf("Hello world\n") and exit you should find you receive that string back in your calling (UI) program. Does that work?

            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