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 719 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.
  • H Offline
    H Offline
    HyEISN
    wrote on 16 Jan 2024, 08:48 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 J 2 Replies Last reply 16 Jan 2024, 09:34
    0
    • H HyEISN
      16 Jan 2024, 08:48

      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 16 Jan 2024, 09:34 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

      H 1 Reply Last reply 16 Jan 2024, 10:07
      0
      • R Redman
        16 Jan 2024, 09:34

        @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

        H Offline
        H Offline
        HyEISN
        wrote on 16 Jan 2024, 10:07 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.

        T 1 Reply Last reply 16 Jan 2024, 10:19
        0
        • H HyEISN
          16 Jan 2024, 10:07

          @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.

          T Offline
          T Offline
          TomZ
          wrote on 16 Jan 2024, 10:19 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
          • H HyEISN
            16 Jan 2024, 08:48

            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!

            J Offline
            J Offline
            JonB
            wrote on 16 Jan 2024, 12:56 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

            1/5

            16 Jan 2024, 08:48

            • Login

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