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
3 Posts 2 Posters 40 Views 1 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 Online
    PerdrixP Online
    Perdrix
    wrote last edited by
    #1

    Is it correct to write:

    if (qApp)
    {
         // Do gui stuff
    }
    else
    {
        // Do command line stuff
    }
    

    Thanks, David

    JonBJ 1 Reply Last reply
    0
    • PerdrixP Perdrix

      Is it correct to write:

      if (qApp)
      {
           // Do gui stuff
      }
      else
      {
          // Do command line stuff
      }
      

      Thanks, David

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

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

      1 Reply Last reply
      0
      • PerdrixP Online
        PerdrixP Online
        Perdrix
        wrote last edited by
        #3

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

        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