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 6.8k 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.
  • P Offline
    P Offline
    pmh4514
    wrote on last edited by A Former User
    #1

    If I want to subclass QApplication in order to add new methods (as opposed to simply implementing the virtual methods) how can I then access my new method elsewhere in the project?

    ie. with the following, I can't simply then say QApplication::SomeCustomMethod()

    class MyApplication : public QApplication
    {
    public:
        MyApplication(int &argc, char **argv);
    
        void SomeCustomMethod();
       .... etc
    }
    
    1 Reply Last reply
    0
    • K Offline
      K Offline
      kegon
      wrote on last edited by kegon
      #2

      That's right, you would use MyApplication::SomeCustomMethod() because SomeCustomMethod() is not a member function of QApplication.

      In your MyApplication object you can call SomeCustomMethod() or externally, you can call the function on an object of type MyApplication.

      Read up on C++ inheritance.

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

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

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

          Hi,

          Out of curiosity, what kind of method do you have in mind ?

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

          P 1 Reply Last reply
          1
          • SGaistS SGaist

            Hi,

            Out of curiosity, what kind of method do you have in mind ?

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

            I'm probably just going about things wrong.
            I want to fire up my app, read configuration settings, setup a bunch of "shared" methods about configuration and mode of operation and then launch the desired "MainWindow" instance depending on mode of operation. So I'd be referencing things stored in "MyApplication" almost like global variables.

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

              Wouldn't QSettings be more suitable for that ?

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

              P 2 Replies Last reply
              1
              • 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