Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Checking if running as command-line (QCoreApplication) or GUI
Qt 6.12 LTS is out! See what's new in the release blog

Checking if running as command-line (QCoreApplication) or GUI

Scheduled Pinned Locked Moved Unsolved General and Desktop
18 Posts 4 Posters 330 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.
  • PerdrixP Perdrix

    @JonB I build a GUI application which instantiates a QApplication in its main(), and a completely different piece of code is used for the command line application which creates a QCoreApplication.

    So I think that qApp will work in that case?

    JonBJ Offline
    JonBJ Offline
    JonB
    wrote last edited by
    #4

    @Perdrix
    It may depend on platform or what you mean by command line. Have you just tested linking your command line program with a piece of code which goes if (qApp) and verified the final executable is still a command-line/console application?

    1 Reply Last reply
    0
    • PerdrixP Perdrix

      @JonB I build a GUI application which instantiates a QApplication in its main(), and a completely different piece of code is used for the command line application which creates a QCoreApplication.

      So I think that qApp will work in that case?

      SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote last edited by
      #5

      @Perdrix said in Checking if running as command-line (QCoreApplication) or GUI:

      @JonB I build a GUI application which instantiates a QApplication in its main(), and a completely different piece of code is used for the command line application which creates a QCoreApplication.

      So I think that qApp will work in that case?

      Hi,

      Something is not clear: are you starting both the CLI and the GUI from the same main function ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • PerdrixP Offline
        PerdrixP Offline
        Perdrix
        wrote last edited by
        #6

        They are completely independent executables each with their own main()

        1 Reply Last reply
        0
        • Christian EhrlicherC Offline
          Christian EhrlicherC Offline
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote last edited by
          #7

          As you can see here: https://doc.qt.io/qt-6/qapplication.html#qApp - qApp is not valid (undefined) when no QApplication instance exixst. When you simply look at the definition you will see what is happening:

          #define qApp (static_cast<QApplication *>(QCoreApplication::instance()))
          

          So no, it will not work and you will have to do it by yourself by calling dynamic_cast<> on the QCoreApplication::instance()

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

          1 Reply Last reply
          1
          • PerdrixP Offline
            PerdrixP Offline
            Perdrix
            wrote last edited by
            #8

            @Christian-Ehrlicher Well that's quite a surprise - from the documentation I had expected the macro to use a dynamic_cast<>

            Christian EhrlicherC 1 Reply Last reply
            0
            • PerdrixP Perdrix

              @Christian-Ehrlicher Well that's quite a surprise - from the documentation I had expected the macro to use a dynamic_cast<>

              Christian EhrlicherC Offline
              Christian EhrlicherC Offline
              Christian Ehrlicher
              Lifetime Qt Champion
              wrote last edited by Christian Ehrlicher
              #9

              @Perdrix said in Checking if running as command-line (QCoreApplication) or GUI:

               from the documentation I had expected the macro to use a dynamic_cast<>
              

              Why? It's clearly stated:

              "so only valid when the unique application object is a QApplication."

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

              1 Reply Last reply
              0
              • PerdrixP Offline
                PerdrixP Offline
                Perdrix
                wrote last edited by
                #10

                Which clearly said to me that it would be null if the application object is just a QCoreApplication...

                So the docs really could be clearer (state that is it using a static_cast<>), or change the code to use a dynamic_cast<>!

                Christian EhrlicherC 1 Reply Last reply
                0
                • PerdrixP Perdrix

                  Which clearly said to me that it would be null if the application object is just a QCoreApplication...

                  So the docs really could be clearer (state that is it using a static_cast<>), or change the code to use a dynamic_cast<>!

                  Christian EhrlicherC Offline
                  Christian EhrlicherC Offline
                  Christian Ehrlicher
                  Lifetime Qt Champion
                  wrote last edited by Christian Ehrlicher
                  #11

                  @Perdrix said in Checking if running as command-line (QCoreApplication) or GUI:

                  Which clearly said to me that it would be null if the application object is just a QCoreApplication...

                  undefined and a nullptr are two completely different things in c++ - the docs are very clear here.
                  https://en.cppreference.com/cpp/language/ub

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

                  1 Reply Last reply
                  0
                  • PerdrixP Offline
                    PerdrixP Offline
                    Perdrix
                    wrote last edited by
                    #12

                    The docs say nothing about undefined.

                    1 Reply Last reply
                    0
                    • Christian EhrlicherC Offline
                      Christian EhrlicherC Offline
                      Christian Ehrlicher
                      Lifetime Qt Champion
                      wrote last edited by Christian Ehrlicher
                      #13

                      "only valid" = undefined if the precondition is not met.

                      /edit:
                      valid pointer - a pointer to an object which is not yet deleted and has the correct class
                      invalid / not valid pointer - a pointer to an object which was already deleted or has an incorrect class
                      nullptr - a pointer to null

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

                      1 Reply Last reply
                      0
                      • PerdrixP Offline
                        PerdrixP Offline
                        Perdrix
                        wrote last edited by
                        #14

                        I still think the wording is open to my interpretation, however I have now defined my own convenience macro:

                        //
                        // Define a convenience macro for checking if we're using a Qt GUI or command line application
                        // 
                        #define qtGUI (dynamic_cast<QApplication *>(QCoreApplication::instance()))
                        
                        1 Reply Last reply
                        0
                        • JonBJ JonB

                          @Perdrix
                          I imagine that is a reasonable test if you want to do a Qt call, as in: "is there a QApplication object?"

                          A global pointer referring to the unique application object. It is equivalent to QCoreApplication::instance(), but cast as a QApplication pointer, so only valid when the unique application object is a QApplication.

                          However, are you intending to use single-source with two separate compilations/links, one for GUI and one for console? Because if you are thinking you can do just one compilation/link and decide at runtime whether to run as GUI or command line you are going to stall. Under Windows at least --- which ISTR you use --- an executable is linked as either "windows" (GUI) or "console" (command-line) and you are stuck with that at runtime.

                          PerdrixP Offline
                          PerdrixP Offline
                          Perdrix
                          wrote last edited by
                          #15

                          After further experiments this seems to be a non-starter. I wanted to use a message box for GUI and cout for command line, but of course the command line code isn't going to be linked with the libraries necessary to use QMessageBox.

                          Hey ho! Compile time checks ...

                          JonBJ 1 Reply Last reply
                          0
                          • PerdrixP Perdrix

                            After further experiments this seems to be a non-starter. I wanted to use a message box for GUI and cout for command line, but of course the command line code isn't going to be linked with the libraries necessary to use QMessageBox.

                            Hey ho! Compile time checks ...

                            JonBJ Offline
                            JonBJ Offline
                            JonB
                            wrote last edited by
                            #16

                            @Perdrix
                            This is the sort of thing I was trying to discuss with you once you start trying to use qApp from a command-line executable.

                            I wanted to use a message box for GUI and cout for command line, but of course the command line code isn't going to be linked with the libraries necessary to use QMessageBox.

                            You implement this requirement via pointers-to-functions or subclassing-virtual-methods. It's a common paradigm. So that your code calls something like utilty.alert("Message") and that ends up calling either the GUI or the command-line implementation depending what function alert() pointer/virtual resolves to as set in construction/start-up. That keeps the command-line from needing to include/link with anything GUI.

                            1 Reply Last reply
                            0
                            • PerdrixP Offline
                              PerdrixP Offline
                              Perdrix
                              wrote last edited by
                              #17

                              Indeed so.

                              JonBJ 1 Reply Last reply
                              0
                              • PerdrixP Perdrix

                                Indeed so.

                                JonBJ Offline
                                JonBJ Offline
                                JonB
                                wrote last edited by JonB
                                #18

                                @Perdrix So you can do your "use a message box for GUI and cout for command line" if you wish. Just saying.

                                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
                                • Unsolved