Invoke Method without using string method name?
-
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
-
Just create a second signal which you only connect for the specific object
-
Just create a second signal which you only connect for the specific object
@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.
-
So again: how do you know which is the 'special' object and why can't you connect another signal to this object then?
-
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
@Taytoo do you mean this method ?
https://doc.qt.io/qt-5/qmetaobject.html#invokeMethodInvoke method has a functor overload
https://doc.qt.io/qt-5/qmetaobject.html#invokeMethod-5which is very refactor friendly and does not rely on char*
-
@Taytoo do you mean this method ?
https://doc.qt.io/qt-5/qmetaobject.html#invokeMethodInvoke method has a functor overload
https://doc.qt.io/qt-5/qmetaobject.html#invokeMethod-5which is very refactor friendly and does not rely on char*
@J-Hilk
Invoke method has a functor overload
https://doc.qt.io/qt-5/qmetaobject.html#invokeMethod-5Like 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-Hilk
Invoke method has a functor overload
https://doc.qt.io/qt-5/qmetaobject.html#invokeMethod-5Like 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?
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.
-
But what's the difference to connecting to a separate signal then in this case?
-
But what's the difference to connecting to a separate signal then in this case?
@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?
-
At the place where you call the invoke function - why can't you simply connect the signal there and emit your signal?
-
At the place where you call the invoke function - why can't you simply connect the signal there and emit your signal?
@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().