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. Invoke Method without using string method name?
Forum Updated to NodeBB v4.3 + New Features

Invoke Method without using string method name?

Scheduled Pinned Locked Moved Unsolved General and Desktop
11 Posts 3 Posters 1.5k 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.
  • Christian EhrlicherC Online
    Christian EhrlicherC Online
    Christian Ehrlicher
    Lifetime Qt Champion
    wrote on last edited by
    #2

    Just create a second signal which you only connect for the specific object

    Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
    Visit the Qt Academy at https://academy.qt.io/catalog

    T 1 Reply Last reply
    2
    • Christian EhrlicherC Christian Ehrlicher

      Just create a second signal which you only connect for the specific object

      T Offline
      T Offline
      Taytoo
      wrote on last edited by
      #3

      @Christian-Ehrlicher Each receiver object is from same class - a list of MyClass objects - so signal/slot logic is same. They actually queue work to background thread continuously, and background thread notifies the respective Object of completion.

      I thought about providing object reference as part of Signal parameter, but that means All receivers' completion slot will be invoked for each job completion, which has its own overhead.

      1 Reply Last reply
      0
      • Christian EhrlicherC Online
        Christian EhrlicherC Online
        Christian Ehrlicher
        Lifetime Qt Champion
        wrote on last edited by
        #4

        So again: how do you know which is the 'special' object and why can't you connect another signal to this object then?

        Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
        Visit the Qt Academy at https://academy.qt.io/catalog

        1 Reply Last reply
        0
        • T Taytoo

          I have a scenario where a slot needs to be invoked for a 'specific' object only, so classic emit won't work since it will invoke slots of All connected objects. Note that invoker is on a different thread and each receiver object also is on a separate thread.

          The only way to invoke a slot on a specific object is to call InvokeMethod with object reference and method/slot name. However, I don't really like the approach of typing method names, as it's error prone especially during refactoring. Is there a more hard-wired method? like the new Connection technique in which you can explicitly mention slot e.g. &MyClass::SlotToInvoke

          J.HilkJ Online
          J.HilkJ Online
          J.Hilk
          Moderators
          wrote on last edited by
          #5

          @Taytoo do you mean this method ?
          https://doc.qt.io/qt-5/qmetaobject.html#invokeMethod

          Invoke method has a functor overload
          https://doc.qt.io/qt-5/qmetaobject.html#invokeMethod-5

          which is very refactor friendly and does not rely on char*


          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.

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

            @Taytoo do you mean this method ?
            https://doc.qt.io/qt-5/qmetaobject.html#invokeMethod

            Invoke method has a functor overload
            https://doc.qt.io/qt-5/qmetaobject.html#invokeMethod-5

            which is very refactor friendly and does not rely on char*

            T Offline
            T Offline
            Taytoo
            wrote on last edited by
            #6

            @J-Hilk

            Invoke method has a functor overload
            https://doc.qt.io/qt-5/qmetaobject.html#invokeMethod-5

            Like this?
            QMetaObject::invokeMethod(objRef, &MyClasst::SlotToInvoke);

            Just thought of one limitation, if Object calls disconnect() - during shutdown etc, then it wouldn't really have any affect on the above code and SlotToInvoke would still be invoked, right?

            J.HilkJ 1 Reply Last reply
            0
            • T Taytoo

              @J-Hilk

              Invoke method has a functor overload
              https://doc.qt.io/qt-5/qmetaobject.html#invokeMethod-5

              Like this?
              QMetaObject::invokeMethod(objRef, &MyClasst::SlotToInvoke);

              Just thought of one limitation, if Object calls disconnect() - during shutdown etc, then it wouldn't really have any affect on the above code and SlotToInvoke would still be invoked, right?

              J.HilkJ Online
              J.HilkJ Online
              J.Hilk
              Moderators
              wrote on last edited by
              #7

              @Taytoo

              Like this?
              QMetaObject::invokeMethod(objRef, &MyClasst::SlotToInvoke);

              yes,

              Just thought of one limitation, if Object calls disconnect() - during shutdown etc, then it wouldn't really have any affect on the above code and SlotToInvoke would still be invoked, right?

              no, same as the char based InvokeMethod, this only executes when the program execution reaches this line, it's not a connect.

              It's equivalent to objRef.slotToInvoke() and only makes sense if you pass it a QueuedConnection or BlockingConnection type as 3rd parameter.


              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.

              1 Reply Last reply
              0
              • Christian EhrlicherC Online
                Christian EhrlicherC Online
                Christian Ehrlicher
                Lifetime Qt Champion
                wrote on last edited by
                #8

                But what's the difference to connecting to a separate signal then in this case?

                Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                Visit the Qt Academy at https://academy.qt.io/catalog

                T 1 Reply Last reply
                0
                • Christian EhrlicherC Christian Ehrlicher

                  But what's the difference to connecting to a separate signal then in this case?

                  T Offline
                  T Offline
                  Taytoo
                  wrote on last edited by
                  #9

                  @Christian-Ehrlicher said in Invoke Method without using string method name?:

                  But what's the difference to connecting to a separate signal then in this case?

                  I have 10s of MyClass objects that post requests to BackgroundThread. MyClass has Job Completion slot which is invoked when job is completed. Also, when MyClass object is cleaning up, it calls disconnect() to ensure no further slots are invoked. The object reference is pass while queuing the job, so BackgroundThread has each object's reference.

                  To have a separate signal is not really possible in this case, since all objects are of the same type. Only option is to either pass a object reference while emitting signal, so all instances can compare the reference in slot and ignore/process the signal. Other option is to use invokeMethod, but then I'd have to replace disconnect() call with a boolean flag, which is set during cleanup and if a slot is invoked, then it simply returns.

                  What's the performance of emit when there are say 20 objects with slots connected? Is invokeMethod's functor variant faster then the string variant and all binding done during compile time?

                  1 Reply Last reply
                  0
                  • Christian EhrlicherC Online
                    Christian EhrlicherC Online
                    Christian Ehrlicher
                    Lifetime Qt Champion
                    wrote on last edited by
                    #10

                    At the place where you call the invoke function - why can't you simply connect the signal there and emit your signal?

                    Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                    Visit the Qt Academy at https://academy.qt.io/catalog

                    T 1 Reply Last reply
                    0
                    • Christian EhrlicherC Christian Ehrlicher

                      At the place where you call the invoke function - why can't you simply connect the signal there and emit your signal?

                      T Offline
                      T Offline
                      Taytoo
                      wrote on last edited by
                      #11

                      @Christian-Ehrlicher said in Invoke Method without using string method name?:

                      At the place where you call the invoke function - why can't you simply connect the signal there and emit your signal?

                      You mean do a connect() inside the emitting code? Isn't it supposed to be the other way around i.e. receiver calls connect().

                      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