Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Missing "this" and "sender" pointers in Qml



  • It seems like using an item's id is the only way to obtain a reference to the Item (without resorting to "parent.children"):

    @Item {
    id: myId
    MouseArea {
    ...
    onClicked: someParent.someSignal(myId)
    }
    }@

    If I have a selection of several Items that need to emit the same signal "someParent.someSignal", then inventing a new id for each item just for passing it to the signal looks a bit tedious.

    To "code less", it would be nice to have something similar to the "this" pointer used in C++:
    @
    onClicked: someParent.someSignal(this)
    @

    Also, there is no "sender" pointer that could be used in an Item that receives someSignal.

    I hope these features are planned in some future release...
    Btw, console.log(this) prints out: "[object global]" - what is that? I could not find it in the docs...


  • Moderators

    [quote author="ivoryZ" date="1354164599"]I hope these features are planned in some future release...[/quote]

    You can suggest a feature on JIRA and/ or Qt development mailing list. I think this is a valid point, I miss such a feature, too.

    In your example, you can probably use the mouse event in the signal:
    @
    onClicked: someParent.someSignal(mouse)
    @
    But that is just one small, corner case.



  • See http://doc.qt.digia.com/qt/qdeclarativejavascript.html for information.
    It's not as simple as you'd think to support the "this" keyword in most JavaScript expressions, but please do file a suggestion if one doesn't already exist.

    Cheers,
    Chris.



  • i think that on embeded functions in the declarative objects, it is so easy has adding a preparser that creates temporary and transparent id for the functions, so that we can use this in many functions, but execution performance is equal.

    Am i wrong or it is in fact this way easy for most of the cases?


Log in to reply