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. How to properly subclass QApplication and access new methods elsewhere?
Forum Updated to NodeBB v4.3 + New Features

How to properly subclass QApplication and access new methods elsewhere?

Scheduled Pinned Locked Moved Solved General and Desktop
16 Posts 6 Posters 7.0k Views 3 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.
  • SGaistS SGaist

    Wouldn't QSettings be more suitable for that ?

    P Offline
    P Offline
    pmh4514
    wrote on last edited by
    #7

    if what I were doing were as straightforward as I described, perhaps :)

    1 Reply Last reply
    0
    • SGaistS SGaist

      Wouldn't QSettings be more suitable for that ?

      P Offline
      P Offline
      pmh4514
      wrote on last edited by
      #8

      basically i was trying to take advantage of the fact that it is already a singleton in the application.. I suppose I could move all my "stuff" into another class and make it a singleton if this isn't an appropriate use of subclassing QApplication

      1 Reply Last reply
      0
      • P pmh4514

        right but SomeCustomMethod() is not static, so how can it simply be referenced by MyApplication:: ?

        K Offline
        K Offline
        kegon
        wrote on last edited by
        #9

        @pmh4514

        I was referring to the scope of the method; because you mentioned calling QApplication::SomeCustomMethod() but that method does not exist in QApplication.

        You need an object unless it is a static function.

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #10

          Well technically you can do it however from a cleanliness point view, it's rather an abuse.

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0
          • M Offline
            M Offline
            mpergand
            wrote on last edited by mpergand
            #11

            You simply need to add this method:

            static MyApplication* instance() { return static_cast<MyApplication*>(QApplication::instance()); }
            

            then you can do:

            MyApplication* myApp=MyApplication::instance();
            myApp->SomeCustomMethod();
            
            1 Reply Last reply
            0
            • Paul ColbyP Offline
              Paul ColbyP Offline
              Paul Colby
              wrote on last edited by Paul Colby
              #12

              Ignoring whether this is a "good thing" to do or not... if the method is not static, you should be able to:

              MyApplication * const myApp = qobject_cast<MyApplication *>(QCoreApplication::instance());
              if (myApp != NULL) {
                  myApp->SomeCustomMethod();
              }
              
              kshegunovK 1 Reply Last reply
              0
              • Paul ColbyP Paul Colby

                Ignoring whether this is a "good thing" to do or not... if the method is not static, you should be able to:

                MyApplication * const myApp = qobject_cast<MyApplication *>(QCoreApplication::instance());
                if (myApp != NULL) {
                    myApp->SomeCustomMethod();
                }
                
                kshegunovK Offline
                kshegunovK Offline
                kshegunov
                Moderators
                wrote on last edited by
                #13

                @Paul-Colby said in how to properly subclass QApplication and access new methods elsewhere?:

                if the method is not static

                In fact, you're correct either way, even more so. Otherwise:

                QApplication app;
                
                MyApplication::instance()->someMethod();
                

                get's pretty ugly pretty fast. ;)

                Read and abide by the Qt Code of Conduct

                1 Reply Last reply
                1
                • P Offline
                  P Offline
                  pmh4514
                  wrote on last edited by
                  #14

                  ugly and obtuse indeed. I'm going to re-think this. thanks!

                  kshegunovK 1 Reply Last reply
                  1
                  • P pmh4514

                    ugly and obtuse indeed. I'm going to re-think this. thanks!

                    kshegunovK Offline
                    kshegunovK Offline
                    kshegunov
                    Moderators
                    wrote on last edited by
                    #15

                    @pmh4514 said in how to properly subclass QApplication and access new methods elsewhere?:

                    ugly and obtuse indeed. I'm going to re-think this. thanks!

                    Well, it's the same with a singleton anyway. If you need a global state of your application QApplication is as good a place as any I suppose, I think you should rather rethink how to pass the data around without resorting to using a singleton. For example your settings object (which you might create in main()) can raise a few signals and if you need you can propagate those signals around to other objects (i.e. through signal forwarding). Ultimately, the interested parties could just subscribe to whatever they're interested in.

                    Read and abide by the Qt Code of Conduct

                    P 1 Reply Last reply
                    2
                    • kshegunovK kshegunov

                      @pmh4514 said in how to properly subclass QApplication and access new methods elsewhere?:

                      ugly and obtuse indeed. I'm going to re-think this. thanks!

                      Well, it's the same with a singleton anyway. If you need a global state of your application QApplication is as good a place as any I suppose, I think you should rather rethink how to pass the data around without resorting to using a singleton. For example your settings object (which you might create in main()) can raise a few signals and if you need you can propagate those signals around to other objects (i.e. through signal forwarding). Ultimately, the interested parties could just subscribe to whatever they're interested in.

                      P Offline
                      P Offline
                      pmh4514
                      wrote on last edited by
                      #16

                      @kshegunov

                      Thanks, good insights.

                      I generally use the singleton in my case as it is modeling/controlling an external piece of hardware, of which there can only be one.

                      I've been doing C++ much longer than Qt.. Still wrapping my head around "The Qt Way" for lots of things!

                      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