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. [SOLVED] Which method of the main Widget returns the QApplication instance?
Forum Updated to NodeBB v4.3 + New Features

[SOLVED] Which method of the main Widget returns the QApplication instance?

Scheduled Pinned Locked Moved General and Desktop
12 Posts 5 Posters 32.2k 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
    jesuisbenjamin
    wrote on last edited by
    #1

    I cannot figure out which method of eg. QWidget one can use to get hold of the QApplication's instance. Is there one?
    Otherwise, should I pass the QApplication as parent of the main widget?

    Thanks.
    B.

    1 Reply Last reply
    0
    • O Offline
      O Offline
      octal
      wrote on last edited by
      #2

      If you want to get a pointer to your QApplication, you can either use :

      • qApp global pointer
      • QApplication::instance() static function
      1 Reply Last reply
      0
      • G Offline
        G Offline
        goetz
        wrote on last edited by
        #3

        The main widget usually does not have a parent.

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

        1 Reply Last reply
        0
        • J Offline
          J Offline
          jesuisbenjamin
          wrote on last edited by
          #4

          Thanks guys for your answers :)

          1 Reply Last reply
          0
          • L Offline
            L Offline
            lgeyer
            wrote on last edited by
            #5

            [quote author="octal" date="1314826374"]If you want to get a pointer to your QApplication, you can either use :

            • qApp global pointer
            • QApplication::instance() static function[/quote]

            I just want to add that QApplication does not have an instance() method, its base class, QCoreApplication, has. This means that a QCoreApplication* is returned, which has to be cast properly.
            @
            QApplication* application = static_cast<QApplication *>(QApplication::instance());
            @

            1 Reply Last reply
            0
            • O Offline
              O Offline
              octal
              wrote on last edited by
              #6

              Yes, qApp performs a cast :

              @#define qApp (static_cast<QApplication *>(QCoreApplication::instance()))@

              However, the doc states :

              bq. Returns a pointer to the application's QCoreApplication (or QApplication) instance.

              1 Reply Last reply
              0
              • A Offline
                A Offline
                andre
                wrote on last edited by
                #7

                [quote author="octal" date="1314861746"]Yes, qApp performs a cast :

                @#define qApp (static_cast<QApplication *>(QCoreApplication::instance()))@

                However, the doc states :

                bq. Returns a pointer to the application's QCoreApplication (or QApplication) instance.[/quote]

                Did you notice that in QCoreApplication, the qApp macro is also defined (line 69 in 4.7.3)?

                @
                #define qApp QCoreApplication::instance()
                @

                So, if you #include <QCoreApplication>, you will end up with the define above, but if you #include <QApplication> you will get the version you quote.

                1 Reply Last reply
                0
                • J Offline
                  J Offline
                  jesuisbenjamin
                  wrote on last edited by
                  #8

                  What worked for me was using pythonic @QtCore.QCoreApplication.instance()@ because @QtGui.qApp@ returned some QApplication object, but it wasn't the instance I created (it didn't have the attributes I had given it).

                  Cheers :)

                  1 Reply Last reply
                  0
                  • J Offline
                    J Offline
                    jesuisbenjamin
                    wrote on last edited by
                    #9

                    PS: it's my opinion that it would make sense if QWidgets had a getApplication() method.

                    1 Reply Last reply
                    0
                    • L Offline
                      L Offline
                      lgeyer
                      wrote on last edited by
                      #10

                      Because?

                      (There can only be exactly one QApplication/QCoreApplication instance, which is accesible from everywhere using qApp() or QCoreApplication::instance() anyways).

                      1 Reply Last reply
                      0
                      • J Offline
                        J Offline
                        jesuisbenjamin
                        wrote on last edited by
                        #11

                        Well you can't run more than one QApplication, and Widgets don't exist without a QApplication right? So in a sense the QApplication is a "parent" of all widgets. It's seems more object-oriented to me. It's my logic, but I don't claim it's universal :)

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

                          The object oriented pattern is called "singleton". And it's object oriented design to get that singleton instance by means of a static method of that class, and not by some unrelated other class. You will need the Q(Core)Application method anyways, as you can have an instance of that without any widgets. So no, an getApplication() method for QWidget, does not make any sense.

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

                          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