4

I'm having an issue with QML/JS integration.

I have a javascript list variable stored in a .js file. I want to write to this variable from one QML file, and read it from another QML file. I cannot seem to find a solution to this. I've over-simplified my actual code to make it comprehensible!

// writeValue.QML
import "../javascript/storedValue.js" as StoredValue
...
MouseArea{
    onClicked{
        StoredValue.value.push(1)
    }
}

// readValue.QML
import "../javascript/storedValue.js" as StoredValue
...
Text{
    text : StoredValue.value
}

//storedValue.js
var value = []

I have tried using '.pragma library' and not using it, to no avail.

What happens is the writeValue.QML writes successfully, so [1, 1, ,1, ...]. Whereas readValue.QML just finds an empty list, [].

1 Answer 1

6

Just put .pragma library at the beginning of the JS file. In this way, there will be only one instance imported by QML components.

Remember however that no update signal is issued when a var property changes. If you want to have global var with update support, you should export a custom QObject via setContextProperty() on the C++ side.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.