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. Console output: GUI vs Cmd Line
Forum Updated to NodeBB v4.3 + New Features

Console output: GUI vs Cmd Line

Scheduled Pinned Locked Moved Solved General and Desktop
4 Posts 3 Posters 437 Views 2 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.
  • S Offline
    S Offline
    SlyPig
    wrote on last edited by
    #1

    I have a Windows application that can run as a GUI (when no cmd line options are specified) or as a cmd line application (when cmd line options are specified). When running as a cmd line application, I sometimes need to output text. For example, if the user opens a console window and types "app -v", I want to output the application's version. The only way I can get text output seems to be if I put "CONFIG += console" in my .pro file. This would be OK except that when I run as a GUI (by double-clicking the .exe), I get this annoying console popping up as well as my app. I've searched the web about ways to fix this, but have not been able to find an answer. I've tried std::cout, std::cerr, qDebug(), QTextStream (see code below), but all results are the same.

    What I would like to have is a GUI that runs without a console window popping up and be able to output text when running as a cmd line application. The code below is a simple example of what I'm trying to do.

    QTextStream& qStdOut()
    {
        static QTextStream ts( stdout );
        return ts;
    }
    
    int main(int argc, char *argv[])
    {
        QApplication a(argc, argv);
    
        if (argc > 1) // User included command line arguments 
        {
            // This only works if "CONFIG += console" is in the .pro file
            qStdOut() << "Output some text here" << endl;
        }
        else // User did not specify command line arguments
        {
            // If "CONFIG += console" is in the .pro file, then a
            // console window also pops up when the main window is
            // shown (i.e. no cmd line arguments specified)
            MainWindow w;
            w.show();
            return a.exec();
        }
    }
    
    1 Reply Last reply
    0
    • K Offline
      K Offline
      khzimmer
      wrote on last edited by khzimmer
      #2

      Hi,

      if you just want to output some simple information you could use QCommandLineParser and its showHelp() method.

      Or do it like this:

      #include <iostream>
      
      //...
      
      auto myText = QStringLiteral ("Hello  world!");
      
      auto myTranslatedText = QObject::tr ("Hello  world!");
      
        std::cout << myText.toUtf8 ().data () << std::endl;
        
        std::cout << myTranslatedText.toUtf8 ().data () << std::endl;
      
      1 Reply Last reply
      0
      • kkoehneK Offline
        kkoehneK Offline
        kkoehne
        Moderators
        wrote on last edited by
        #3

        I think this was just recently discussed at https://forum.qt.io/topic/134576/configure-qapplication-to-work-like-qcoreapplication .

        Bottom line: The decision whether a windows executable is attached to a console application or not is a link time decision. There's some ways around this (you can check out e.g. what the Qt Installer Framework does). But the last suggestion in above thread is to just have a .exe and a .cmd entry point for your application, maybe this works for you too?

        Director R&D, The Qt Company

        1 Reply Last reply
        2
        • S Offline
          S Offline
          SlyPig
          wrote on last edited by
          #4

          Thank you for your reference to the other forum topic which somehow didn't show up in my search. It looks like they wanted to do the same thing I am trying to do here. I would rather not have to create two applications (one GUI, one cmd line), but not sure if there are any other options at this point.

          1 Reply Last reply
          0

          • Login

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