Reputation: 41
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, [].
Upvotes: 4
Views: 2405
Reputation: 7518
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.
Upvotes: 6