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. Open console with GUI only if Qapp is called with true argument.
Forum Updated to NodeBB v4.3 + New Features

Open console with GUI only if Qapp is called with true argument.

Scheduled Pinned Locked Moved Unsolved General and Desktop
12 Posts 4 Posters 1.1k 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.
  • F Offline
    F Offline
    filipdns
    wrote on last edited by
    #1

    Hello,

    I nned help because I want to have my QApplication is opened with a console only when I'm calling it with for example :

    C:/app.exe true
    

    The goal is to have only one exe but with argument true, the console can be open to show qdebug messages, and not with arguments false.

    I know how to have the console on with includung CONFIG+= console inside pro file, but how to disable it with argument false?

    Thank you for your help

    Philippe

    JonBJ 1 Reply Last reply
    0
    • F filipdns

      Hello,

      I nned help because I want to have my QApplication is opened with a console only when I'm calling it with for example :

      C:/app.exe true
      

      The goal is to have only one exe but with argument true, the console can be open to show qdebug messages, and not with arguments false.

      I know how to have the console on with includung CONFIG+= console inside pro file, but how to disable it with argument false?

      Thank you for your help

      Philippe

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

      @filipdns
      This has been asked before. A Windows executable is either a console application or a UI application: this is determined at link time (and I imagine it's what CONFIG+= console affects). You cannot alter this at runtime for the same executable.

      1 Reply Last reply
      0
      • F Offline
        F Offline
        filipdns
        wrote on last edited by
        #3

        hello,

        I found the solution using FreeConsole();

        best regards

        Philippe

        JonBJ 1 Reply Last reply
        0
        • F filipdns

          hello,

          I found the solution using FreeConsole();

          best regards

          Philippe

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

          @filipdns
          Then presumably you do not use CONFIG+= console? Or you do still use that if you use the Windows SDK FreeConsole()? I can only think you are build a Windows UI app and switching on/off the display of a console, but not just building a console app?

          Doesn't the console flash up and show, before you hide it? This is discussed in, say, https://stackoverflow.com/questions/66185000/how-can-i-do-the-freeconsole-but-faster-on-c, and elsewhere.

          1 Reply Last reply
          0
          • F Offline
            F Offline
            filipdns
            wrote on last edited by
            #5

            In pro file I add CONFIG+= console and when I don't want to have the console appear, then I pass my argument to false, example C:/app.exe false

            and inside my app I add:

            int main(int argc, char *argv[])
            {
                QApplication a(argc, argv);
                bool debug=false;
            
                debug=QString(argv[1])=="false"?false:true;
            
                if(!debug){
                    FreeConsole();
            
                }
            ...
            
            JonBJ 1 Reply Last reply
            1
            • F filipdns

              In pro file I add CONFIG+= console and when I don't want to have the console appear, then I pass my argument to false, example C:/app.exe false

              and inside my app I add:

              int main(int argc, char *argv[])
              {
                  QApplication a(argc, argv);
                  bool debug=false;
              
                  debug=QString(argv[1])=="false"?false:true;
              
                  if(!debug){
                      FreeConsole();
              
                  }
              ...
              
              JonBJ Offline
              JonBJ Offline
              JonB
              wrote on last edited by
              #6

              @filipdns
              Great, so at least I understand. You have a UI application (QApplication) and you have CONFIG+= console. I didn't know that was allowed. I guess that makes a Windows UI application also create and show a separate window which is a console? Maybe (I don't know) the same could be achieved without CONFIG+= console and instead having if (debug) AllocConsole().

              BTW, have you ensured your code works both ways if run outside Qt Creator?

              S 1 Reply Last reply
              1
              • JonBJ JonB

                @filipdns
                Great, so at least I understand. You have a UI application (QApplication) and you have CONFIG+= console. I didn't know that was allowed. I guess that makes a Windows UI application also create and show a separate window which is a console? Maybe (I don't know) the same could be achieved without CONFIG+= console and instead having if (debug) AllocConsole().

                BTW, have you ensured your code works both ways if run outside Qt Creator?

                S Offline
                S Offline
                SimonSchroeder
                wrote on last edited by
                #7

                I am with @JonB. A console app on Windows cannot have an event loop and widgets. And a GUI application does not show its console by default. @filipdns have you tried to show a window with your current approach? I doubt that it works. From my understanding CONFIG needs to be without console to have GUI application on Windows (just as @JonB said). Then you need to use AllocConsole() to create a console instead of using FreeConsole(). An additional benefit is that there will be no flickering of the console when it is not enabled. This should be the proper way.

                F 1 Reply Last reply
                2
                • M Offline
                  M Offline
                  mchinand
                  wrote on last edited by
                  #8

                  How does QtCreator's 'Run in terminal' option work to open a Windows console when starting an application within QtCreator?

                  JonBJ 1 Reply Last reply
                  0
                  • M mchinand

                    How does QtCreator's 'Run in terminal' option work to open a Windows console when starting an application within QtCreator?

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

                    @mchinand
                    Well, the point is that Creator is doing that opening, and attaching the outputs as necessary. It does not change your application to do so. Which is why I suggested the OP test whether what works presently works outside Creator. Creator is doing something like AllocConsole() (Windows) to create that terminal and attach it.

                    1 Reply Last reply
                    1
                    • S SimonSchroeder

                      I am with @JonB. A console app on Windows cannot have an event loop and widgets. And a GUI application does not show its console by default. @filipdns have you tried to show a window with your current approach? I doubt that it works. From my understanding CONFIG needs to be without console to have GUI application on Windows (just as @JonB said). Then you need to use AllocConsole() to create a console instead of using FreeConsole(). An additional benefit is that there will be no flickering of the console when it is not enabled. This should be the proper way.

                      F Offline
                      F Offline
                      filipdns
                      wrote on last edited by
                      #10

                      @SimonSchroeder Thank you for your suggestion but with -=console in pro file and Alloconsole(), I have terminal openning yes but no qDebug or console.log information display on it.

                      The +=console method with Freeconsole() work fine for me even I have a very short time a console appearing when I'm starting my application when I don't want a console.

                      JonBJ 1 Reply Last reply
                      0
                      • F filipdns

                        @SimonSchroeder Thank you for your suggestion but with -=console in pro file and Alloconsole(), I have terminal openning yes but no qDebug or console.log information display on it.

                        The +=console method with Freeconsole() work fine for me even I have a very short time a console appearing when I'm starting my application when I don't want a console.

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

                        @filipdns said in Open console with GUI only if Qapp is called with true argument.:

                        Alloconsole(), I have terminal openning yes but no qDebug or console.log information display on i

                        You would presumably have to do stuff to redirect stdout/err etc. to the allocated console.

                        S 1 Reply Last reply
                        0
                        • JonBJ JonB

                          @filipdns said in Open console with GUI only if Qapp is called with true argument.:

                          Alloconsole(), I have terminal openning yes but no qDebug or console.log information display on i

                          You would presumably have to do stuff to redirect stdout/err etc. to the allocated console.

                          S Offline
                          S Offline
                          SimonSchroeder
                          wrote on last edited by
                          #12

                          @JonB said in Open console with GUI only if Qapp is called with true argument.:

                          You would presumably have to do stuff to redirect stdout/err etc. to the allocated console.

                          You certainly have to: https://stackoverflow.com/a/57241985/6954968

                          1 Reply Last reply
                          1

                          • Login

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