Checking if running as command-line (QCoreApplication) or GUI
-
Is it correct to write:
if (qApp) { // Do gui stuff } else { // Do command line stuff }Thanks, David
@Perdrix
I imagine that is a reasonable test if you want to do a Qt call, as in: "is there aQApplicationobject?"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.
-
@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?
-
@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?
@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 ?