转http://blog.csdn.net/jokerjhm/article/details/6736860
The ListElement element defines a data item in a ListModel. More…
ListElement在ListModel中定义一个数据项。
Detailed DescriptionList elements are defined inside ListModel definitions, and represent items in a list that will be displayed using ListView or Repeater items.
列表元素被定义在ListModel定义的内部,描述了列表中项,列表通过ListView或Repeater来显示。
List elements are defined like other QML elements except that they contain a collection of role definitions instead of properties. Using the same syntax as property definitions, roles both define how the data is accessed and include the data itself.
列表元素的定义类似QML元素,除了它们包含role定义集合来代替属性。用和属性相同的语法来定义,role定义了数据如何被访问和包括数据自己。
The names used for roles must begin with a lower-case letter and should be common to all elements in a given model. Values must be simple constants; either strings (quoted and optionally within a call to QT_TR_NOOP), boolean values (true, false), numbers, or enumeration values (such as AlignText.AlignHCenter).
role的名称必须要小写字母开头,被所有的model中元素共有。数值必须是简单的常量;strings(quoted and optionally within a call to QT_TR_NOOP),boolean(true,false)numbers,或枚举值(比如AlignText.AlignHCenter)
Referencing RolesThe role names are used by delegates to obtain data from list elements. Each role name is accessible in the delegate’s scope, and refers to the corresponding role in the current element. Where a role name would be ambiguous to use, it can be accessed via the model property (e.g., model.cost instead of cost).
delegate用role的名称来获取列表元素的数据。每个role的名称在delegate的范围内都是可访问的,并引用当前元素相关的role。role的名称的使用可能会有引起歧义的地方,可以通过model的属性来访问(例如,model.cost代替cost).
Example UsageThe following model defines a series of list elements, each of which contain “name” and “cost” roles and their associated values.
下面的model定义了一系列的列表元素,每个列表元素包含了名为“name”和“cost” 的role和它们的相关联的值。
[javascript] view plaincopy
[list=1]
[]ListModel {
[] id: fruitModel
[]
[] ListElement {
[] name: “Apple”
[] cost: 2.45
[] }
[] ListElement {
[] name: “Orange”
[] cost: 3.25
[] }
[] ListElement {
[] name: “Banana”
[] cost: 1.95
[] }
[]}
[/list]
The delegate obtains the name and cost for each element by simply referring to name and cost:
通过简单的引用name和cost,delegate获取每个元素的name和cost:
ListView {
anchors.fill: parent
model: fruitModel
delegate: Row {
Text {
text: "Fruit: " + name }
Text { text: “Cost: $” + cost }
}
}