Handling Qt initialization status



  • Hi,
    I would like to know if there's a way to check if Qt is able to initialize successfully (→ the GUI should show up), or not (→ handle the error, give basic CLI capabilities). In GTK+, one would use gtk_init_check(), how can accomplish a similar thing in Qt? :)

    bye ;)

    P.S.: forgot to specify this, I need it to work only on Linux/X11 platforms.



  • What do you mean by Qt is initialized successfully? What should be initialized? You could use QCoreApplication::startingUp() which tells you, if a QCoreApplication instance exists. But that you should know in your main function.

    What else could be of interest?



  • Gerolf, he meant: Test if the environment allows a GUI application, if yes start QApplication and run the GUI version, otherwise start QCoreApplication an run a console version (much like our beloved gvim does :-) )

    On Unix/Linux with X11 you can use this trick:

    @
    int main(int argc, char **argv)
    {
    #ifdef Q_WS_X11
    bool useGUI = getenv("DISPLAY") != 0;
    #else
    bool useGUI = true;
    #endif
    QApplication app(argc, argv, useGUI);

     if (useGUI) {
        // start GUI version
        ...
     } else {
        // start non-GUI version
        ...
     }
     return app.exec();
    

    }
    @

    (from "QApplication":http://doc.qt.nokia.com/stable/qapplication.html#QApplication-2 docs)



  • Yes, Volker is right, that's quite what I need.
    I have seen that trick before in the Qt examples, but it's unfortunate that I need to separate the cases according to an environment variabile. It would be nice if Qt also allowed me, for example, to let the QApplication parse the arguments and recognize any occurrence of a "-display" switch, then try to set everything up accordingly, and, in case of failure, throw an exception which I can handle as I wish.
    As I have seen from the Qt source code, instead, exit() gets called, so the closest thing I can do here is to register an atexit() handler...

    Thanks for your responses anyway! :)



  • [quote author="jmc-88" date="1295358771"]Yes, Volker is right, that's quite what I need.
    I have seen that trick before in the Qt examples, but it's unfortunate that I need to separate the cases according to an environment variabile. It would be nice if Qt also allowed me, for example, to let the QApplication parse the arguments and recognize any occurrence of a "-display" switch, then try to set everything up accordingly, and, in case of failure, throw an exception which I can handle as I wish.
    As I have seen from the Qt source code, instead, exit() gets called, so the closest thing I can do here is to register an atexit() handler...
    [/quote]

    QApplication recognizes the -display command line option (see the QApplication ctor docs for that), which is why you have to pass argc and argv to its ctor. Apart from this, you're right:

    • Qt doesn't use exceptions
    • Q(Core)Application doesn't have a "isValid()" method or so
    • QApplication simply calls exit(1) if it cannot successfully connect to the X server (which is what you want to do in the 99.9999% of cases).

    You can work around this by opening the connection by hand (using Xlib) and using the QApplication ctor that takes a Display *.



  • Right, that's what I thought in first place. But that means I have to filter out any "-display" switch myself. Ok, I can live with that, but I thought there was a better way to do this. :)

    Thanks to all of you for your time and your answers. ;)
    bye :)

    P.S.: I know QApplication recognizes "-display". The thing I didn't know is if there was some way of letting Qt handle the arguments, and then react according to the initialization status.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.