Is it possible to determine on run-time if a QCoreApplication or a QApplication is running?
Is it possible to determine on run-time if a QCoreApplication or a QApplication is running?
There is QCoreApplication::startingUp() if that's what you are after.
Unfortunately not! I would need to determine if a console (QCoreApplication) or GUI (QApplication) has been instantiated.
You mean you want to know whether you have a QCoreApplication instance or a QApplication instance? That's quite easy actually:
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).Qt Code:
To copy to clipboard, switch view to plain text mode
doberkofler (16th September 2010)
Is there a way to determine if I'm running a console or a GUI app?
QApplication::type() looks very promising but failing that I suspect QApplication::topLevelWidgets() should be non-empty for a GUI app.
doberkofler (16th September 2010)
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.
I meant a GUI app, not a GUI so this is a perfectly good GUI app:
Qt Code:
#include <QtGui> int main(int argc, char **argv){ return app.exec(); }To copy to clipboard, switch view to plain text mode
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).
I make of with the following that seems to work fine for me:
Qt Code:
bool isGuiApp() { bool aIsGuiApp = false; if (aApplication) { } return aIsGuiApp; }To copy to clipboard, switch view to plain text mode
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?
Are you writing a library or is it for your own application?
A library for a set of own applications
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.
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.
So you are instantiating a QCoreApplication yourself?
No, I'm using the QTEST_APPLESS_MAIN() macro.
So you don't have any application object - neither console nor non-console. In this case QCoreApplication::startingUp() would be enough.
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.
Bookmarks