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. Configure QApplication to work like QCoreApplication?
QtWS25 Last Chance

Configure QApplication to work like QCoreApplication?

Scheduled Pinned Locked Moved Unsolved General and Desktop
qcoreapplicatioqapplicationcommand
8 Posts 5 Posters 1.3k Views
  • 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
    Dariusz
    wrote on 23 Feb 2022, 10:12 last edited by
    #1

    Hey

    Is there a way to configure QApplication to behave like a command line?

    I'm trying to build app that can support both options, but when I run in the command line I can get a crash as some widgets complain that there is no QApplication.

    I know its more of an architecture issue that I build - not having enough experience in building command-line apps.

    So I was wondering if there are any flags that i can set to have it "hot fixed" without the need of rewriting big chunks of my app.

    Right now my gui app in cmd never really "starts", or shows up & hides. I tried to make my own console on windows but that works on 1 pc but breaks on another... sigh.

    Regards
    Dariusz

    1 Reply Last reply
    0
    • C Offline
      C Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on 23 Feb 2022, 10:17 last edited by
      #2

      No, this is not possible. But you can instantiate either a QCoreApplication or QApplication in your main.cpp depending on e.g. a command line option.

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      D 1 Reply Last reply 23 Feb 2022, 10:23
      2
      • C Christian Ehrlicher
        23 Feb 2022, 10:17

        No, this is not possible. But you can instantiate either a QCoreApplication or QApplication in your main.cpp depending on e.g. a command line option.

        D Offline
        D Offline
        Dariusz
        wrote on 23 Feb 2022, 10:23 last edited by Dariusz
        #3

        @Christian-Ehrlicher I see, thank you!

        One more question then... I'm passing WIN32 in my cmake when building the app. This seems to remove output that would go to console... is there a way to get it back?

        On windows I've guild console back using this>

        #include "consoleapi.h"
        #include "consoleapi2.h"
        #include "WinBase.h"
        
        void Console() {
            AllocConsole();
            FILE *pFileCon = NULL;
            pFileCon = freopen("CONOUT$", "w", stdout);
        
            COORD coordInfo;
            coordInfo.X = 130;
            coordInfo.Y = 9000;
        
            SetConsoleScreenBufferSize(GetStdHandle(STD_OUTPUT_HANDLE), coordInfo);
            SetConsoleMode(GetStdHandle(STD_OUTPUT_HANDLE), ENABLE_QUICK_EDIT_MODE | ENABLE_EXTENDED_FLAGS);
        }
        

        It runs fine on my pc, but on another pc it shows up & dies instnatly.

        C 1 Reply Last reply 23 Feb 2022, 10:30
        0
        • D Dariusz
          23 Feb 2022, 10:23

          @Christian-Ehrlicher I see, thank you!

          One more question then... I'm passing WIN32 in my cmake when building the app. This seems to remove output that would go to console... is there a way to get it back?

          On windows I've guild console back using this>

          #include "consoleapi.h"
          #include "consoleapi2.h"
          #include "WinBase.h"
          
          void Console() {
              AllocConsole();
              FILE *pFileCon = NULL;
              pFileCon = freopen("CONOUT$", "w", stdout);
          
              COORD coordInfo;
              coordInfo.X = 130;
              coordInfo.Y = 9000;
          
              SetConsoleScreenBufferSize(GetStdHandle(STD_OUTPUT_HANDLE), coordInfo);
              SetConsoleMode(GetStdHandle(STD_OUTPUT_HANDLE), ENABLE_QUICK_EDIT_MODE | ENABLE_EXTENDED_FLAGS);
          }
          

          It runs fine on my pc, but on another pc it shows up & dies instnatly.

          C Offline
          C Offline
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on 23 Feb 2022, 10:30 last edited by
          #4

          @Dariusz said in Configure QApplication to work like QCoreApplication?:

          One more question then... I'm passing WIN32 in my cmake when building the app. This seems to remove output that would go to console... is there a way to get it back?

          Don't pass WIN32 then if you want a console.

          Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
          Visit the Qt Academy at https://academy.qt.io/catalog

          D 1 Reply Last reply 23 Feb 2022, 10:31
          0
          • C Christian Ehrlicher
            23 Feb 2022, 10:30

            @Dariusz said in Configure QApplication to work like QCoreApplication?:

            One more question then... I'm passing WIN32 in my cmake when building the app. This seems to remove output that would go to console... is there a way to get it back?

            Don't pass WIN32 then if you want a console.

            D Offline
            D Offline
            Dariusz
            wrote on 23 Feb 2022, 10:31 last edited by
            #5

            @Christian-Ehrlicher I want both, An app that can be run from console with arguments and app that can run from exe as gui. But running from the console does not print anything in to console :- (

            1 Reply Last reply
            0
            • H Offline
              H Offline
              hskoglund
              wrote on 23 Feb 2022, 13:00 last edited by
              #6

              Hi, I had more or less the same problem, solved it by creating a small console (non-gui) Qt app that first uses QSharedMemory to see if the normal gui app is running.
              If it's not, then I launch the gui app from the console app, with the arguments the console app got on the command line.

              1 Reply Last reply
              0
              • K Offline
                K Offline
                kkoehne
                Moderators
                wrote on 23 Feb 2022, 13:20 last edited by
                #7

                @Dariusz said in Configure QApplication to work like QCoreApplication?:

                I want both, An app that can be run from console with arguments and app that can run from exe as gui. But running from the console does not print anything in to console :- (

                Well, I think you can also have a normal graphical app without WIN32 (that is, SUBSYSTEM:console on linker command line). Just that it will always be 'attached'to the console.

                If you want to avoid this, you can try a stunt like the installer framework does that 'restarts' itself to detach from the console: https://code.qt.io/cgit/installer-framework/installer-framework.git/tree/src/sdk/main.cpp

                Director R&D, The Qt Company

                1 Reply Last reply
                1
                • S Offline
                  S Offline
                  SimonSchroeder
                  wrote on 24 Feb 2022, 07:19 last edited by
                  #8

                  When I researched having both a GUI and command line application on Windows I stumbled upon how Microsoft does it itself with devenv. They provide a .exe and a .com at the same time. If you call your software myapp then you should compile the GUI application to myapp.exe and the command line application to myapp.com. When typing myapp on the command line it will launch the .com version.

                  What we did in addition is to have the two application call each other depending on the command line parameters.

                  This is a better user experience than attaching a console.

                  With qmake use an additional CONFIG to select between GUI and console (e.g. CONFIG+=console when calling qmake). In your main() have #ifdefs to switch between command line and GUI during compilation.

                  1 Reply Last reply
                  1
                  • S SGaist referenced this topic on 9 Nov 2023, 20:14

                  6/8

                  23 Feb 2022, 13:00

                  • Login

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