View Full Version : Determine if QCoreApplication or QApplication is running

15th September 2010, 23:54
Is it possible to determine on run-time if a QCoreApplication or a QApplication is running?

16th September 2010, 00:13
There is QCoreApplication::startingUp() if that's what you are after.

16th September 2010, 00:27
Unfortunately not! I would need to determine if a console (QCoreApplication) or GUI (QApplication) has been instantiated.

16th September 2010, 01:01
You mean you want to know whether you have a QCoreApplication instance or a QApplication instance? That's quite easy actually:

bool hasGuiApp = (qobject_cast<QApplication*>(QCoreApplication::instance())!=0);
But the fact that you have a QApplication instance doesn't mean you have a GUI. QApplication can be used with a console app as well (see the third parameter to one of QApplicaiton constructors).

16th September 2010, 08:55
Is there a way to determine if I'm running a console or a GUI app?

16th September 2010, 09:23
QApplication::type() looks very promising but failing that I suspect QApplication::topLevelWidgets() should be non-empty for a GUI app.

16th September 2010, 11:32
I suspect QApplication::topLevelWidgets() should be non-empty for a GUI app.
You can have a perfectly good GUI application without any windows being open. I'd go for type(). On X11 you could check if you have a Display structure available.

17th September 2010, 00:30
I must be missing the obvious... How can you have a GUI without any top level widgets? This list will contain top level widgets even if they are hidden from view.

17th September 2010, 00:33
How can you have a GUI without any top level widgets?
I meant a GUI app, not a GUI so this is a perfectly good GUI app:

#include <QtGui>
int main(int argc, char **argv){
QApplication app(argc, argv); // GUI

return app.exec();

But even if we talk about an app that has a GUI this is still possible - you can have an application that only has a system tray icon, I doubt that's considered a "top-level widget".

Besides, I think the OP wants to be able to check the availability of the GUI at an arbitrary moment so it might happen that he does it when no windows are present in the application (visible or not).

17th September 2010, 09:14
I make of with the following that seems to work fine for me:

bool isGuiApp()
bool aIsGuiApp = false;
QApplication* aApplication = qobject_cast<QApplication*>(QCoreApplication::instance());
if (aApplication)
aIsGuiApp = (aApplication->type() == QApplication::GuiClient);

return aIsGuiApp;

17th September 2010, 11:11
By the way, what do you need it for?

17th September 2010, 11:40
I was looking for a way to make my error handling working in console and GUI applications. If I'm running without a GUI, I will no longer try to offer the user a "nice" graphical error dialog.
Any better way to do this?

17th September 2010, 11:53
Are you writing a library or is it for your own application?

17th September 2010, 12:01
A library for a set of own applications

17th September 2010, 12:13
If you don't intend to ever link with QtGui and run a non-gui application then you don't have to worry about it. It's a bit strange combination anyway.

17th September 2010, 12:17
Maybe I got it wrong but I for example this is exactly my setup when compiling my QTest based test cases that need to be linked with QtGui but internally QTest instantiates a console application.

17th September 2010, 12:38
So you are instantiating a QCoreApplication yourself?

17th September 2010, 13:11
No, I'm using the QTEST_APPLESS_MAIN() macro.

17th September 2010, 13:18
So you don't have any application object - neither console nor non-console. In this case QCoreApplication::startingUp() would be enough.

17th September 2010, 13:24
This is correct! I was nevertheless looking for a more generic way that would also allow me to work if a QCoreApplication would be instantiated explicitly.

17th September 2010, 19:45
Then qobject_cast<QApplication*>(QCoreApplication::instance()) works.