Reputation: 860
If a ListView contains user-defined properties, these properties can be referenced in a binding for model
but they can not for anything inside the delegate. Why is this?
The docs seem to say that a Component should be able see properties in enclosing scopes where it was declared.
import QtQuick 2.12
import QtQuick.Controls 2.12
ApplicationWindow {
visible:true
ListView {
orientation: ListView.Vertical; height: 300; width: 100
property var myCount: 3
property var myMessage: "Hello"
Component {
id: myComp
Text {text: myMessage} // ReferenceError: myMessage is not defined
}
model: myCount // this works
delegate: myComp
}
}
(In my real application, the ListView is a component (.qml file) and the invoker needs to pass in information needed to configure the delegate; not literal text like in this example, but information for a nested ListView.)
Thanks for any help...
Upvotes: 1
Views: 367
Reputation: 244301
The variables in QML have a scope, in your case when using myMessage without reference these indicating that the variable belongs to the Text item.
# ...
Component {
id: myComp
Text {text: myMessage}
}
# ...
So the solution is to use the ListView id as a reference:
# ...
ListView {
id: lv
orientation: ListView.Vertical; height: 300; width: 100
property var myCount: 3
property var myMessage: "Hello"
Component {
id: myComp
Text {text: lv.myMessage}
}
model: myCount // this works
delegate: myComp
}
# ...
Upvotes: 2