Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Handling Qt initialization status
Forum Updated to NodeBB v4.3 + New Features

Handling Qt initialization status

Scheduled Pinned Locked Moved General and Desktop
6 Posts 4 Posters 5.0k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    jmc-88
    wrote on last edited by
    #1

    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.

    1 Reply Last reply
    0
    • G Offline
      G Offline
      giesbert
      wrote on last edited by
      #2

      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?

      Nokia Certified Qt Specialist.
      Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

      1 Reply Last reply
      0
      • G Offline
        G Offline
        goetz
        wrote on last edited by
        #3

        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)

        http://www.catb.org/~esr/faqs/smart-questions.html

        1 Reply Last reply
        0
        • J Offline
          J Offline
          jmc-88
          wrote on last edited by
          #4

          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! :)

          1 Reply Last reply
          0
          • D Offline
            D Offline
            dangelog
            wrote on last edited by
            #5

            [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 *.

            Software Engineer
            KDAB (UK) Ltd., a KDAB Group company

            1 Reply Last reply
            0
            • J Offline
              J Offline
              jmc-88
              wrote on last edited by
              #6

              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.

              1 Reply Last reply
              0

              • Login

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved