If someMethod() is a slot you can do this:
Qt Code:
QMetaObejct::invokeMethod(&obj[10], "someMethod", Qt::QueuedConnection);To copy to clipboard, switch view to plain text mode
If someMethod() is a slot you can do this:
Qt Code:
QMetaObejct::invokeMethod(&obj[10], "someMethod", Qt::QueuedConnection);To copy to clipboard, switch view to plain text mode
You mean because obj[10] could have been deleted by its thread?
Because that is the only inherently unsafe part in that call.
That of course also accesses "obj" without any guard, so it has the same problem as the direct method call.
The only difference is that "someMethod" will be executed by the object's owner thread, which also has to run an event loop to be able to do that.
Cheers,
_
Last edited by anda_skoa; 3rd June 2016 at 08:46.
thx for explaing. First I want to say that I dont't want to use invokeMethod cause there is not compile-time errors if i write the functionname wrong. Just want to point that out, you must not comment that
Then i have two questions. Why always people say use Signals&Slots when it seems there is like no advantage in that, when the only possilbility that the call is unsafe is that the obj gets deleted? Is it cause you have then Connectiontypes available?
This leads my to my second question. What connectiontype would be equal to obj[10].someMethod(); ?
Well, you don't have that with the SIGNAL/SLOT macros either.
Using signal/slot or invokeMethod makes it easy to cross thread boundaries, make the target thread execute a method.
Especially nice when communicating from a worker thread to the main thread.
Qt::DirectConnection
Cheers,
_
"Well, you don't have that with the SIGNAL/SLOT macros either."
I will get an compile error if the Slot does not exist that i type in with connect. With invokemethod i do not get an compile-error.
Bookmarks