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. Terminal Output Of Application Outside of Creator
Forum Updated to NodeBB v4.3 + New Features

Terminal Output Of Application Outside of Creator

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 3 Posters 250 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.
  • D Offline
    D Offline
    daviddev
    wrote on last edited by
    #1

    I wish to see output statements in a Windows 10 desktop GUI application in a terminal when running in a release and debug builds outside of creator. The output statements work perfectly fine within Qt Creator.

    I am running Qt 6.9.2 on MSVC2022 64bit with Cmake.

    #include "mainwindow.h"
    #include <QApplication>
    #include <iostream>
    #include <QProcess>
    #include <QDir>
    #include <QDebug>
    int main(int argc, char *argv[])
    {
        std::cout << "Standard output text" << std::endl;
        std::cerr << "Standard error text" << std::endl;
        QTextStream out(stdout);
        out << "Test Qtext";
        QApplication a(argc, argv);
        MainWindow w;
        w.show();
        return a.exec();
    }
    
    

    I have tried running QProcess's that would run concurrently to the application without success. Also, I have tried the recommended "Run in Terminal" option with and without enabling the internal terminal of Creator and both ways fail to show outputs. When I uncheck the "Run in terminal" option, my output is the expected text below.

    Hello from standard output!
    Hello error message.
    Test Qtext
    

    Would anyone have experience with this or have a hunch?

    jsulmJ 1 Reply Last reply
    0
    • D daviddev

      I wish to see output statements in a Windows 10 desktop GUI application in a terminal when running in a release and debug builds outside of creator. The output statements work perfectly fine within Qt Creator.

      I am running Qt 6.9.2 on MSVC2022 64bit with Cmake.

      #include "mainwindow.h"
      #include <QApplication>
      #include <iostream>
      #include <QProcess>
      #include <QDir>
      #include <QDebug>
      int main(int argc, char *argv[])
      {
          std::cout << "Standard output text" << std::endl;
          std::cerr << "Standard error text" << std::endl;
          QTextStream out(stdout);
          out << "Test Qtext";
          QApplication a(argc, argv);
          MainWindow w;
          w.show();
          return a.exec();
      }
      
      

      I have tried running QProcess's that would run concurrently to the application without success. Also, I have tried the recommended "Run in Terminal" option with and without enabling the internal terminal of Creator and both ways fail to show outputs. When I uncheck the "Run in terminal" option, my output is the expected text below.

      Hello from standard output!
      Hello error message.
      Test Qtext
      

      Would anyone have experience with this or have a hunch?

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @daviddev Do you start your application from a terminal?

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • D Offline
        D Offline
        daviddev
        wrote on last edited by daviddev
        #3

        @jsulm Thank you for replying. I have launched the application without any arguments from cmd and Windows Powershell not as an administrator without them displaying any output.

        //Example Launches Without Output From cmd
        C:\Users\username\path> FrontEnd.exe
        C:\Users\username\path> start FrontEnd.exe
        C:\Users\username\path> start /B FrontEnd.exe
        
        

        Logs do display text when I launch the applications and redirect their output to a log. Logging is not an acceptable solution with the messages I wish displayed in runtime unfortunately.

        //Launch With Redirected Output
        C:\Users\username\path>FrontEnd.exe > output.txt
        

        When I simply click on the executable through file explorer, no terminal pops up and there is also no output. However, there is no supporting QProcess that would launch another terminal, so I expect that. In Qt creator, I get output when I run with and without the internal terminal.

        From this behavior, my hunch is that it is a really simple fix or something subtle. It seems the terminal either receives the output and does not display it either because it is not listening, I need to enable special permissions for a windows terminal in the program, or there is a blocker of some kind.

        Would you have any ideas?

        JonBJ 1 Reply Last reply
        0
        • D daviddev

          @jsulm Thank you for replying. I have launched the application without any arguments from cmd and Windows Powershell not as an administrator without them displaying any output.

          //Example Launches Without Output From cmd
          C:\Users\username\path> FrontEnd.exe
          C:\Users\username\path> start FrontEnd.exe
          C:\Users\username\path> start /B FrontEnd.exe
          
          

          Logs do display text when I launch the applications and redirect their output to a log. Logging is not an acceptable solution with the messages I wish displayed in runtime unfortunately.

          //Launch With Redirected Output
          C:\Users\username\path>FrontEnd.exe > output.txt
          

          When I simply click on the executable through file explorer, no terminal pops up and there is also no output. However, there is no supporting QProcess that would launch another terminal, so I expect that. In Qt creator, I get output when I run with and without the internal terminal.

          From this behavior, my hunch is that it is a really simple fix or something subtle. It seems the terminal either receives the output and does not display it either because it is not listening, I need to enable special permissions for a windows terminal in the program, or there is a blocker of some kind.

          Would you have any ideas?

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

          @daviddev
          You have a Windows "UI" program, i.e. linked as a "windows" rather than a "console" application.

          I think you will find: when a Windows UI application is launched, whether from a terminal or icon-shortcut, that stdout and stderr are attached to "nowhere", so anything written to them "disappears". If it works from FrontEnd.exe > output.txt that implies that the calling shell has redirected stdout to the file so it stays like that when it runs and you get output there.

          The stuff inside Creator with/without terminal is special code in Creator so that's different.

          I am not sure what exactly you want to achieve for what situation(s). Logging to file sounds like a better option than stdout/err for a UI program anyway. Under Windows you might also try downloading something like dbwin, dbwin32 or whatever the modern replacement is and running that --- that displays any text sent via Win OutputDebugString(), it may also capture some other stuff I'm not sure. Your program can also call Win32 AllocConsole() to allocate a visible console window (like a terminal) and that will have stdout/err attached, instead of your attempted "supporting QProcess". Finally a UI program could create e.g. a permanently visible modeless dialog/window with, say, a QPlainTextEdit in it to which you send all your output messages for it to display.

          1 Reply Last reply
          1
          • D Offline
            D Offline
            daviddev
            wrote on last edited by
            #5

            @JonB Thank you for your post. It turns out the AllocConsole() was the solution. It was a Windows problem I was unfamiliar with rather than QT. Have a great week!

            1 Reply Last reply
            0
            • D daviddev has marked this topic as solved on

            • Login

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