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 10 Aug 2022, 08:22 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

    J 1 Reply Last reply 10 Aug 2022, 08:25
    0
    • F filipdns
      10 Aug 2022, 08:22

      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

      J Offline
      J Offline
      JonB
      wrote on 10 Aug 2022, 08:25 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 10 Aug 2022, 08:54 last edited by
        #3

        hello,

        I found the solution using FreeConsole();

        best regards

        Philippe

        J 1 Reply Last reply 10 Aug 2022, 09:16
        0
        • F filipdns
          10 Aug 2022, 08:54

          hello,

          I found the solution using FreeConsole();

          best regards

          Philippe

          J Offline
          J Offline
          JonB
          wrote on 10 Aug 2022, 09:16 last edited by JonB 8 Oct 2022, 09:19
          #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 10 Aug 2022, 09:32 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();
            
                }
            ...
            
            J 1 Reply Last reply 10 Aug 2022, 09:51
            1
            • F filipdns
              10 Aug 2022, 09:32

              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();
              
                  }
              ...
              
              J Offline
              J Offline
              JonB
              wrote on 10 Aug 2022, 09:51 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 11 Aug 2022, 07:43
              1
              • J JonB
                10 Aug 2022, 09:51

                @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 11 Aug 2022, 07:43 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 17 Aug 2022, 08:04
                2
                • M Offline
                  M Offline
                  mchinand
                  wrote on 11 Aug 2022, 13:52 last edited by
                  #8

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

                  J 1 Reply Last reply 11 Aug 2022, 14:10
                  0
                  • M mchinand
                    11 Aug 2022, 13:52

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

                    J Offline
                    J Offline
                    JonB
                    wrote on 11 Aug 2022, 14:10 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
                      11 Aug 2022, 07:43

                      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 17 Aug 2022, 08:04 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.

                      J 1 Reply Last reply 17 Aug 2022, 08:10
                      0
                      • F filipdns
                        17 Aug 2022, 08:04

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

                        J Offline
                        J Offline
                        JonB
                        wrote on 17 Aug 2022, 08:10 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 18 Aug 2022, 06:37
                        0
                        • J JonB
                          17 Aug 2022, 08:10

                          @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 18 Aug 2022, 06:37 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

                          1/12

                          10 Aug 2022, 08:22

                          • Login

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