Reputation: 415
Is there a way to get a reference to the calling object in QML? I'm looking at something which is equivalent to the 'this' pointer in QML
Example: Say I have a component which serves as a backend for a graphical element such as a seekbar for a video player. This backend will take in the current and total durations of the video as input and periodically provide an update to the graphical seekbar. Now if there is a signal handler in this backend for a signal which sends current and total durations , it might look something like this:
Connections {
target: sender //this
onSendSeekUpdate()
{
//do something
}
}
Of course I guess this can be implemented in C++ and then imported into QML. But I was just wondering if QML also supports this? So that I can straight away write such hooks in QML.
Upvotes: 5
Views: 5286
Reputation: 7518
In QML you can use any id
as a pointer, as well as any QObject derived type property, so in your code example, we can change the target
of the Connection
dynamically and still use the same var in the signal handler to point on the sender :
Connections {
target: myitem; // change it when you need
onMySignal: {
target.doSomething(); // just use target here as it points on the listened object
// it's just like 'sender()' in Qt/C++
}
}
Not sure it was what you were asking for, but I tried to understand your explanation ;-)
Upvotes: 4