【发布时间】:2017-02-07 00:12:04
【问题描述】:
这是以下代码的结果:
main.qml
import QtQuick 2.8
Item {
Reusable {
index: 1234 // reusable with a custom index
}
ListView {
anchors { fill: parent; margins: 20; topMargin: 50 }
model: 3
// Reusable with an index given by the ListView
delegate: Reusable {
index: index // <- does not work, both 'index' point to
// the index property
}
}
}
Reusable.qml
import QtQuick 2.8
Text {
property int index
text: "Line " + index
}
问题描述:
ListView 在每次迭代时将 0、1、2 等分配给变量 index。但是,因为我将它分配给一个属性,所以这个变量被隐藏了,我无法访问它。
如果我从Reusable.qml 中删除property int index,ListView 可以工作,但在 ListView 之外使用Reusable 将不再工作。
有没有办法分配index: index?
(我可以重命名有效的属性,但我想避免这种情况。)
【问题讨论】: