obj is an array in my code i just simplified it here. Then i would need an array of signals right? When I only want to adress one part of the array eg obj[2], when I emit the signal.
If that works with the array of signals i had a thinking error.
obj is an array in my code i just simplified it here. Then i would need an array of signals right? When I only want to adress one part of the array eg obj[2], when I emit the signal.
If that works with the array of signals i had a thinking error.
There is no such thing as an array of signals.
Maybe you could describe what you are currently trying to solve?
Cheers,
_
mainwindow.cpp
Obj obj[maxObjectCount];
Obj thread[maxObjectCount];
for (int i = 0; i < maxObjectCount; i++) {
obj[i].moveToThread(thread[i])
}
Now i need to call a Function of lets say obj[10] in mainwindow.cpp
Idk know at all how to do that. Nobody can exlain threads that sb understand it seems, maybe nobody understands them I'm thinking ^^
So you need to call a method in an object and you are asking how to emit a signal?
Cheers,
_
this is not threadsafe
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 09: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