Page 1 of 2 12 LastLast
Results 1 to 20 of 21

Thread: Determine if QCoreApplication or QApplication is running

  1. #1
    Join Date
    Oct 2009
    Location
    Vienna, Austria
    Posts
    57
    Thanks
    24
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Determine if QCoreApplication or QApplication is running

    Is it possible to determine on run-time if a QCoreApplication or a QApplication is running?

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Determine if QCoreApplication or QApplication is running

    There is QCoreApplication::startingUp() if that's what you are after.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Oct 2009
    Location
    Vienna, Austria
    Posts
    57
    Thanks
    24
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: Determine if QCoreApplication or QApplication is running

    Unfortunately not! I would need to determine if a console (QCoreApplication) or GUI (QApplication) has been instantiated.

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Determine if QCoreApplication or QApplication is running

    You mean you want to know whether you have a QCoreApplication instance or a QApplication instance? That's quite easy actually:
    Qt Code:
    1. bool hasGuiApp = (qobject_cast<QApplication*>(QCoreApplication::instance())!=0);
    To copy to clipboard, switch view to plain text mode 
    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).
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. The following user says thank you to wysota for this useful post:

    doberkofler (16th September 2010)

  6. #5
    Join Date
    Oct 2009
    Location
    Vienna, Austria
    Posts
    57
    Thanks
    24
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: Determine if QCoreApplication or QApplication is running

    Is there a way to determine if I'm running a console or a GUI app?

  7. #6
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Determine if QCoreApplication or QApplication is running

    QApplication::type() looks very promising but failing that I suspect QApplication::topLevelWidgets() should be non-empty for a GUI app.

  8. The following user says thank you to ChrisW67 for this useful post:

    doberkofler (16th September 2010)

  9. #7
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Determine if QCoreApplication or QApplication is running

    Quote Originally Posted by ChrisW67 View Post
    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.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  10. #8
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Determine if QCoreApplication or QApplication is running

    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.

  11. #9
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Determine if QCoreApplication or QApplication is running

    Quote Originally Posted by ChrisW67 View Post
    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:
    Qt Code:
    1. #include <QtGui>
    2. int main(int argc, char **argv){
    3. QApplication app(argc, argv); // GUI
    4.  
    5. return app.exec();
    6. }
    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).
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  12. #10
    Join Date
    Oct 2009
    Location
    Vienna, Austria
    Posts
    57
    Thanks
    24
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: Determine if QCoreApplication or QApplication is running

    I make of with the following that seems to work fine for me:

    Qt Code:
    1. bool isGuiApp()
    2. {
    3. bool aIsGuiApp = false;
    4. QApplication* aApplication = qobject_cast<QApplication*>(QCoreApplication::instance());
    5. if (aApplication)
    6. {
    7. aIsGuiApp = (aApplication->type() == QApplication::GuiClient);
    8. }
    9.  
    10. return aIsGuiApp;
    11. }
    To copy to clipboard, switch view to plain text mode 

  13. #11
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Determine if QCoreApplication or QApplication is running

    By the way, what do you need it for?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  14. #12
    Join Date
    Oct 2009
    Location
    Vienna, Austria
    Posts
    57
    Thanks
    24
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: Determine if QCoreApplication or QApplication is running

    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?

  15. #13
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Determine if QCoreApplication or QApplication is running

    Are you writing a library or is it for your own application?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  16. #14
    Join Date
    Oct 2009
    Location
    Vienna, Austria
    Posts
    57
    Thanks
    24
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: Determine if QCoreApplication or QApplication is running

    A library for a set of own applications

  17. #15
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Determine if QCoreApplication or QApplication is running

    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.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  18. #16
    Join Date
    Oct 2009
    Location
    Vienna, Austria
    Posts
    57
    Thanks
    24
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: Determine if QCoreApplication or QApplication is running

    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.

  19. #17
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Determine if QCoreApplication or QApplication is running

    So you are instantiating a QCoreApplication yourself?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  20. #18
    Join Date
    Oct 2009
    Location
    Vienna, Austria
    Posts
    57
    Thanks
    24
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: Determine if QCoreApplication or QApplication is running

    No, I'm using the QTEST_APPLESS_MAIN() macro.

  21. #19
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Determine if QCoreApplication or QApplication is running

    So you don't have any application object - neither console nor non-console. In this case QCoreApplication::startingUp() would be enough.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  22. #20
    Join Date
    Oct 2009
    Location
    Vienna, Austria
    Posts
    57
    Thanks
    24
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: Determine if QCoreApplication or QApplication is running

    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.

Similar Threads

  1. Replies: 0
    Last Post: 25th November 2009, 16:07
  2. Replies: 4
    Last Post: 9th November 2009, 22:12
  3. Replies: 4
    Last Post: 1st December 2008, 12:13
  4. Replies: 1
    Last Post: 17th May 2006, 01:23
  5. <QtGui/QApplication> vs. <QApplication>
    By seneca in forum Qt Programming
    Replies: 5
    Last Post: 25th January 2006, 11:58

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.