Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass “this” from a QML element to a JS Function

Tags:

qt

qml

qtquick2

Similar to the this keyword in C++, I’d like to either have a QML element to pass itself into a JS function, or have it set a property on another element to itself. Is this possible?

For example:

Rectangle{
id:theParent

property var theElement

SomeElement{
id:theChild
   MouseArea {
        anchors.fill:parent
        onClicked: {
            someJsFunction(*whatGoesHere*)
            parent.theElement=*whatGoesHere*
        }
    }

Or, Consider this:

Rectangle{
id:theParent

property var theElement

SomeElement{
id:theChild
    }

Then, in SomeElement.qml:

Rectangle{
   MouseArea {
        anchors.fill:parent
        onClicked: {
            someJsFunction(*whatGoesHere*)
            parent.theElement=*whatGoesHere*
        }
}
}

In this case, the *whatGoesHere* would be the instance of SomeElement where these are being called from.

Is this possible in QML? I would think the id property would make sense, but according to the docs, you cannot query the value of the id field, and anyway the id wouldn't be available if my SomeElement was described in a separate file, and the whatGoesHere handling above appeared in that separate file rather than in a particular instance.

like image 252
johnbakers Avatar asked Nov 05 '13 06:11

johnbakers


People also ask

Does QML support JavaScript?

JavaScript ExpressionsQML has a deep JavaScript integration, and allows signal handlers and methods to be defined in JavaScript. Another core feature of QML is the ability to specify and enforce relationships between object properties using property bindings, which are also defined using JavaScript.

How do you write a function in QML?

Define the function in your root-node ( ApplicationWindow ). This will be the last place, QML will look for a name, before it resorts to the C++ -context properties. See here to find out, how the names of variables and functions are resolved in QML.

What is alias in QML?

Unlike an ordinary property definition, which allocates a new, unique storage space for the property, a property alias connects the newly declared property (called the aliasing property) as a direct reference to an existing property (the aliased property).


1 Answers

I have two complementary proposals :

First, for a single usage, pass the ID as it's basically a pointer to the item :

MouseArea {
    id: myClicker;
    onClicked: { callFunc (myClicker); }
}

Then if you need multiple items to share this behavior, that means you're using MVC so the ID will work exactly the same :

Repeater {
    model: 100;
    delegate: MouseArea {
         id: myClicker;
         onClicked: { callFunc (myClicker); }
    }
}

That is the classical part.

But to todo even better if you create your own components, keep in mind to create a 'self' helper property that does the 'this' job properly :

MouseArea { // component root definition
    id: base;

    property var self : base; // bind self on the I
}

Then use it like this :

Repeater {
    model: 100;
    delegate: MyComponent {
         onClicked: { callFunc (self); }
    }
}

Use this as often as you want !

like image 168
TheBootroo Avatar answered Nov 12 '22 16:11

TheBootroo