Results 1 to 20 of 21

Thread: Determine if QCoreApplication or QApplication is running

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    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.

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,360
    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.


  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

    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 

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,360
    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.


  5. #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

    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?

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,360
    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.


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

  8. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,360
    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.


  9. #9
    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.

  10. #10
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,360
    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.


  11. #11
    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.

  12. #12
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,360
    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.


Similar Threads

  1. Replies: 0
    Last Post: 25th November 2009, 15:07
  2. Replies: 4
    Last Post: 9th November 2009, 21:12
  3. Replies: 4
    Last Post: 1st December 2008, 11:13
  4. Replies: 1
    Last Post: 17th May 2006, 00:23
  5. <QtGui/QApplication> vs. <QApplication>
    By seneca in forum Qt Programming
    Replies: 5
    Last Post: 25th January 2006, 10: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.