In qml, How can i get signal sender from slot/function?
-
In qml, How can i get signal sender from slot/function?
-
In QML, AFAIK, you cannot. In C++, you might use the sender() method.
-
I'm looking for the exact same mechanism. I dynamically created objects stored in a javascript list, they all send signals, calling the same function in my QML parent component. how can i know which dynamic object is sending a signal at run time? i would like to get a reference to the dynamic object that is actually sending the signal.
Thank you for your help!
Bill -
What I use in my project is to send an index together with the signal (as one of signal's parameters). Then in the slot, it look for object with that index and voila - I know who sent it ;) Not a perfect solution, but works.
-
I can not rely on index, because I actually destroy some objects and then the index of the remaining object in my qml objects list are no longer valid. All index get shifted.
Wonder if i can simply expose my viewer to QML world and call the sender() function.
Do the qdeclarativeview inherits this sender() method?Bill
[Moderator note: Deleted duplicate post in thread -- mlong]
-
Most probably. sender() is declared in QObject, so most Qt classes have it.
-
[quote author="sierdzio" date="1347991080"]Most probably. sender() is declared in QObject, so most Qt classes have it.[/quote]
True, but I don't think you have access to that from the QML side of things. Wasn't that the whole point of the discussion?
-
Yeah, I somehow misunderstood billouparis's last statement. Although actually, hm. Maybe it could work? If you exposed ::sender() from C++? I try to avoid using sender() in my code, but maybe internal handling by moc would allow that. Could be a bit unreliable, but might work. I think I'll try to make some test app tomorrow, if time allows.
-
hi.. @slerdzlo
please can you send your example code of sending index together with the signal please...
-
Code of my project has evolved much since then, but here you go with an example: "link":https://github.com/sierdzio/closecombatfree/blob/master/src/qmlBase/ccfqmlbasescenario.cpp#L216.
-
Thanku @slerdzlo.. Thanku very much