Qt 为什么可以';ListView委托是否引用ListView的属性?
如果ListView包含用户定义的属性,则这些属性可以在Qt 为什么可以';ListView委托是否引用ListView的属性?,qt,listview,properties,qml,Qt,Listview,Properties,Qml,如果ListView包含用户定义的属性,则这些属性可以在模型的绑定中引用,但不能用于委托内部的任何内容。为什么会这样 似乎是说组件应该能够在声明它的封闭作用域中看到属性 import QtQuick 2.12 import QtQuick.Controls 2.12 ApplicationWindow { visible:true ListView { orientation: ListView.Vertical; height: 300; width: 10
模型
的绑定中引用,但不能用于委托内部的任何内容。为什么会这样
似乎是说组件应该能够在声明它的封闭作用域中看到属性
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
}
}
(在我的实际应用程序中,ListView是一个组件(.qml文件),调用程序需要传入配置委托所需的信息;而不是
如本例中所示的文本,但嵌套ListView的信息。)
感谢您的帮助……在您使用myMessage而不引用时,QML中的变量有一个作用域,这表明变量属于文本项
# ...
Component {
id: myComp
Text {text: myMessage}
}
# ...
因此,解决方案是使用ListView id作为参考:
# ...
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
}
# ...