Repeater QML Type
使用提供的模型实例化若干基于项目的组件。更多
Import Statement: | import QtQuick |
Inherits: |
属性
- count : int
- delegate : Component
- delegateModelAccess : enumeration
- model : var
信号
- itemAdded(int index, Item item)
- itemRemoved(int index, Item item)
方法
- Item itemAt(index)
详细说明
Repeater 类型用于创建大量类似的项目。与其他视图类型一样,Repeater 也有一个model 和一个delegate :对于模型中的每个条目,委托都是在使用模型数据种子的上下文中实例化的。Repeater 项目通常包含在Row 或Column 等定位器类型中,以便直观地定位 Repeater 创建的多个委托项目。
下面的 Repeater 在Row 中创建了三个Rectangle 项目实例:
import QtQuick Row { Repeater { model: 3 Rectangle { width: 100; height: 40 border.width: 1 color: "yellow" } } }
Repeater 的model 可以是任何支持的数据模型。此外,与其他视图的委托一样,Repeater 委托可以访问其在 Repeater 中的索引以及与委托相关的模型数据。详情请参阅delegate 属性文档。
由中继器实例化的项目会作为中继器父节点的子节点按顺序插入。插入会紧接着中继器在父堆叠列表中的位置开始。这样,中继器就可以在布局中使用。例如,下面的中继器项目堆叠在红色矩形和蓝色矩形之间:
Row { Rectangle { width: 10; height: 20; color: "red" } Repeater { model: 10 Rectangle { width: 20; height: 20; radius: 10; color: "green" } } Rectangle { width: 10; height: 20; color: "blue" } }
注: Repeater 项目拥有其实例化的所有项目。删除或动态销毁 Repeater 创建的项目会导致不可预知的行为。
使用 Repeater 时的注意事项
中继器类型会在首次创建中继器时创建其所有委托项。如果存在大量委托项,且并非所有委托项都需要同时可见,那么这种创建方式就会效率低下。如果是这种情况,可以考虑使用其他视图类型,如ListView (只有在滚动到视图时才会创建委托项),或者使用Dynamic Object Creation 方法在需要时创建项目。
此外,请注意 Repeater 是基于Item 的,只能重复Item 派生的对象。例如,它不能用于重复 QtObjects:
// bad code: Item { // Can't repeat QtObject as it doesn't derive from Item. Repeater { model: 10 QtObject {} } }
属性文档
count : int |
该属性保存model 中的项目数。
count
的值并不总是与实例化的delegates 的数量一致;使用itemAt() 检查给定索引处的委托是否存在。如果委托没有实例化,则返回null
。
- 当 Repeater 正在实例化委托时(启动时或由于
model
的更改),会为每个创建的委托发出itemAdded 信号,随后count
属性也会更改。 - 如果 Repeater 不是已完成的可视化层次结构的一部分,
count
会反映模型大小,但不会创建委托。 - 如果 Repeater 因
model
更改而销毁委托,则会为每个委托发出itemRemoved() 信号,随后count
属性也会更改。 - 如果 Repeater 被移出可视化层次结构(例如通过设置
parent = null
),则会销毁委托,每个委托都会发出itemRemoved() 信号,但count
不会改变。
另请参阅 itemAt()、itemAdded() 和itemRemoved()。
delegate : Component |
delegateModelAccess : enumeration |
该属性决定了代表如何访问模型。
常量 | 说明 |
---|---|
DelegateModel.ReadOnly | 禁止代表通过上下文属性、model 对象或必填属性来写模型。 |
DelegateModel.ReadWrite | 允许代表通过上下文属性、model 对象或必填属性写入模型。 |
DelegateModel.Qt5ReadWrite | 允许代表通过model 对象和上下文属性写入模型,但不允许通过必填属性写入模型。 |
默认值为DelegateModel.Qt5ReadWrite
。
另请参阅 Qt Quick#更改模型数据中的模型和视图。
model : var |
为中继器提供数据的模型。
此属性可设置为任何支持的数据模型:
- 一个数字,表示中继器要创建的代表数目
- 模型(例如ListModel 项目或QAbstractItemModel 子类)
- 字符串列表
- 对象列表
模型的类型会影响delegate 所显示的属性。
另请参阅 数据模型。
信号文档
当一个项目被添加到中继器时,就会发出该信号。index 参数包含项目插入中继器的索引,而item 参数则包含已添加的Item 。
注: 相应的处理程序是onItemAdded
。
当项目从中继器中移除时,将发出该信号。index 参数包含项目从中继器中移除时的索引,而item 参数则包含被移除的Item 。
如果item 是由该中继器创建的,则不要保留对它的引用,因为在这种情况下,它将在信号处理后不久被删除。
注: 相应的处理程序是onItemRemoved
。
方法文档
© 2025 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.