Jaruba
Jaruba

Reputation: 1055

QML - Imported JS object not notifiable

I've made a JS file with objects:

myObject {
    myVar: "hello"
}

That I then import in QML:

import "myObject.js" as SomeObjects
Rectangle {
    Text {
        color: "red"
        text: SomeObjects.myObject.myVar        
    }
}

But if I do SomeObjects.myObject.myVar = "goodbye" the Text Component in QML will not be updated. So this must mean that JS objects are not notifiable.

What is the best way to store notifiable variables in an imported file with QML?

Upvotes: 0

Views: 97

Answers (1)

folibis
folibis

Reputation: 12864

Javascript objects don't have such feature like property binding. So the only way is to use QML object. If you have any calculation in JS you can put it in different js file.

import "myObject.js" as SomeObjects

QtObject {
    id: myObject
    property string myVar: ""
    function someCalculation () {
        SomeObjects.somefunction(myVar);
    }
}

Text {
    text: myObject.myVar
}

Component.onCompleted: {
    myObject.myVar = "Hello"
}

Upvotes: 2

Related Questions