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. Check if method existed in subclassed QObject. How?
Forum Updated to NodeBB v4.3 + New Features

Check if method existed in subclassed QObject. How?

Scheduled Pinned Locked Moved Solved General and Desktop
7 Posts 5 Posters 997 Views 2 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.
  • B Offline
    B Offline
    bogong
    wrote on last edited by
    #1

    Hello all!
    Is there any option to determine object method existance in class that subclassing QObject?
    Need something like this:

    SomeObject* oObject = new SomeObject();
    if (oObject has method) {
        ...
    } else {
        ...
    }
    
    J.HilkJ Pl45m4P JonBJ 3 Replies Last reply
    0
    • B bogong

      Hello all!
      Is there any option to determine object method existance in class that subclassing QObject?
      Need something like this:

      SomeObject* oObject = new SomeObject();
      if (oObject has method) {
          ...
      } else {
          ...
      }
      
      J.HilkJ Offline
      J.HilkJ Offline
      J.Hilk
      Moderators
      wrote on last edited by
      #2

      @bogong there is the QMetaObject of your QObject based class, that has for example
      https://doc.qt.io/qt-6/qmetaobject.html#indexOfMethod

      is that what you're looking for ?


      Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


      Q: What's that?
      A: It's blue light.
      Q: What does it do?
      A: It turns blue.

      B 1 Reply Last reply
      2
      • B bogong

        Hello all!
        Is there any option to determine object method existance in class that subclassing QObject?
        Need something like this:

        SomeObject* oObject = new SomeObject();
        if (oObject has method) {
            ...
        } else {
            ...
        }
        
        Pl45m4P Offline
        Pl45m4P Offline
        Pl45m4
        wrote on last edited by
        #3

        @bogong said in Check if method existed in subclassed QObject. How?:

        Is there any option to determine object method existance in class that subclassing QObject?

        This sounds like weird design. Aren't there any other options to do want you want to achieve? Like checking what (sub-)class your current object actually is and then handle it?!


        If debugging is the process of removing software bugs, then programming must be the process of putting them in.

        ~E. W. Dijkstra

        1 Reply Last reply
        2
        • B bogong

          Hello all!
          Is there any option to determine object method existance in class that subclassing QObject?
          Need something like this:

          SomeObject* oObject = new SomeObject();
          if (oObject has method) {
              ...
          } else {
              ...
          }
          
          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by JonB
          #4

          @bogong said in Check if method existed in subclassed QObject. How?:

          if (oObject has method) {

          Change over to Python instead of C++ ;-)

          And note that with C++ you won't be able to call it even if it does

          SomeObject* oObject = new SomeObject();
          if (oObject has method) {
              oObject call method
          

          C++ can only call methods known at compile-time. And either SomeObject does or doesn't have a given method.

          Chris KawaC 1 Reply Last reply
          1
          • JonBJ JonB

            @bogong said in Check if method existed in subclassed QObject. How?:

            if (oObject has method) {

            Change over to Python instead of C++ ;-)

            And note that with C++ you won't be able to call it even if it does

            SomeObject* oObject = new SomeObject();
            if (oObject has method) {
                oObject call method
            

            C++ can only call methods known at compile-time. And either SomeObject does or doesn't have a given method.

            Chris KawaC Online
            Chris KawaC Online
            Chris Kawa
            Lifetime Qt Champion
            wrote on last edited by
            #5

            @JonB Since we're talking about QObjects there is runtime reflection, so you can do:

            int idx = object->metaObject()->indexOfMethod("someMethod()");
            if (idx >= 0)
            {
                QMetaMethod method = object->metaObject()->method(idx);
                method.invoke(object);
            }
            

            but I agree that it smells like bad design. That's something you should probably handle with templates e.g. CRTP or dynamic dispatch. Using runtime reflection in C++ is usually a sign of things going wrong.

            JonBJ 1 Reply Last reply
            2
            • Chris KawaC Chris Kawa

              @JonB Since we're talking about QObjects there is runtime reflection, so you can do:

              int idx = object->metaObject()->indexOfMethod("someMethod()");
              if (idx >= 0)
              {
                  QMetaMethod method = object->metaObject()->method(idx);
                  method.invoke(object);
              }
              

              but I agree that it smells like bad design. That's something you should probably handle with templates e.g. CRTP or dynamic dispatch. Using runtime reflection in C++ is usually a sign of things going wrong.

              JonBJ Offline
              JonBJ Offline
              JonB
              wrote on last edited by JonB
              #6

              @Chris-Kawa
              I know. But I was trying to avoid this/keep it simple for OP. Or change to Python where this is fine :) The OP may genuinely be in a case where he is looking for reflection, or he may not be....

              Certainly the example of SomeObject* oObject = new SomeObject(); is not the usual case where you would be looking for reflection. I assume that would be more likely where you don't know exactly what the object is which is handed to you. It might become clearer what the OP has in mind if he comments.

              1 Reply Last reply
              1
              • J.HilkJ J.Hilk

                @bogong there is the QMetaObject of your QObject based class, that has for example
                https://doc.qt.io/qt-6/qmetaobject.html#indexOfMethod

                is that what you're looking for ?

                B Offline
                B Offline
                bogong
                wrote on last edited by bogong
                #7

                @J-Hilk Thx that's what been seeking. Issue closed

                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