C
PathView QML Type
将模型中的对象沿路径排列。更多...
| Import Statement: | import QtQuick |
| Since: | Qt Quick Ultralite 2.12 |
| Inherits: |
属性
- count : int
- currentIndex : int
- delegate : Component
- model : model
- path : Path
- pathItemCount : int
- preferredHighlightBegin : real
- preferredHighlightEnd : real
关联属性
- isCurrentItem : bool
方法
- void decrementCurrentIndex()
- void incrementCurrentIndex()
详细说明
PathView 用于显示由内置 QML 类型(如 `ListModel `)创建的模型数据,或由继承自 `Qul::ListModel` 的 C++ 自定义模型类定义的数据。
该视图具有一个model 属性,用于定义要显示的数据;以及一个delegate 属性,用于定义数据的显示方式。对于PathView中的每个项目,都会实例化一个delegate 。
注意:与 主流 Qt不同, Qt Quick Ultralite 不支持使用轻扫或滑动手势沿路径移动项目。请使用按钮来更改 PathView 的currentIndex 属性。
例如,如果ContactModel.qml 文件中定义了一个简单的列表模型,如下所示:
ListModel { ListElement { name: "Bill Jones" icon: "qtlogo.png" } ListElement { name: "Jane Doe" icon: "qtlogo.png" } ListElement { name: "John Smith" icon: "qtlogo.png" } }
这些数据可以像这样表示为一个 PathView:
Rectangle { PathView { model: model1 // Note, pathItemCount should be set to a value larger than 0 pathItemCount: 3 path: Path { startX: 120; startY: 100 PathQuad { x: 120; y: 25; controlX: 260; controlY: 75 } PathQuad { x: 120; y: 100; controlX: -20; controlY: 75 } } delegate: Item { id: wrapper height: 100 width: 100 required property string icon required property string name required property int index readonly property bool isCurrent: PathView.isCurrentItem opacity: isCurrent ? 1 : 0.5 // Note: to keep the current item on top of the others change the z value z: isCurrent ? 1 : 0 Image { anchors.horizontalCenter: nameText.horizontalCenter width: 64; height: 64 source: wrapper.icon } Text { id: nameText text: wrapper.name font.pointSize: 16 anchors.bottom: parent.bottom } } } }

PathView 不会自动处理键盘导航,因为导航键取决于路径的形状。请改用按钮在 PathView 中浏览项目,通过调用decrementCurrentIndex 或incrementCurrentIndex 来更改当前索引。
PathView { // ... Button { text: "Previous" onClicked: pathView.decrementCurrentIndex() } Button { text: "Next" onClicked: pathView.incrementCurrentIndex() } // the Keys.* with the focus property is not supported in \QUL // focus: true // Keys.onLeftPressed: decrementCurrentIndex() // Keys.onRightPressed: incrementCurrentIndex() }
不应将状态信息存储在委托中,因为Qt Quick Ultralite 会随时实例化并销毁委托。
Qt Quick Ultralite 会为委托静态分配固定大小,您可以通过pathItemCount 属性定义该大小。该大小应为大于 0 的值,代表路径上任何时刻可能显示的项目最大数量。如果模型中的项目数量少于pathItemCount 属性指定的值,PathView 会相应地调整委托的数量。
PathView 将 `PathView.isCurrentItem ` 属性附加到委托的根项上。在下面的示例中,根委托项可以直接通过 `PathView.isCurrentItem` 访问该附加属性,而子 `nameText ` 对象则需要额外添加限定符才能访问该属性
Component { id: delegate Column { id: wrapper required property url icon required property string name opacity: PathView.isCurrentItem ? 1 : 0.5 Image { anchors.horizontalCenter: nameText.horizontalCenter width: 64; height: 64 source: wrapper.icon } Text { id: nameText text: wrapper.name + (wrapper.PathView.isCurrentItem ? " (current)" : "") font.pointSize: 16 } } }
重要注意事项
- 使用
PathView.isCurrentItem突出显示当前项目,格式为Qt Quick 。Ultralite 目前尚不支持highlightItem。 - PathView 不会自动启用剪切功能。如果视图未被其他项目或屏幕剪切,请将clip: true设置为true,以剪切视口外的项目。
另请参阅 Path 、ListModel 、ListView 、《Qt Quick Ultralite》中的“模型与视图”,以及C++ 数据模型。
属性文档
count : int
该属性存储模型中的项目数量。
currentIndex : int
该属性保存当前项的索引。
delegate : Component
该委托提供了一个模板,用于定义视图所实例化的每个项目。索引作为可访问的index 属性对外暴露。根据ListModel 的类型,模型的属性也可用。
注意: PathView 将根据根项的大小对项目进行布局。
以下是一个委托示例:
delegate: Item { id: wrapper height: 100 width: 100 required property string icon required property string name required property int index readonly property bool isCurrent: PathView.isCurrentItem opacity: isCurrent ? 1 : 0.5 // Note: to keep the current item on top of the others change the z value z: isCurrent ? 1 : 0 Image { anchors.horizontalCenter: nameText.horizontalCenter width: 64; height: 64 source: wrapper.icon } Text { id: nameText text: wrapper.name font.pointSize: 16 anchors.bottom: parent.bottom } } }
model : model
该属性保存为视图提供数据的模型。
模型提供了一组数据,用于为视图创建项目。对于大型或动态数据集,请使用 C++ 模型;对于简单数据集,请使用基于ListModel 类型的 QML 模型。
注意:更改 model 将重置offset和currentIndex ,使其变为0 。
另请参阅 ListModel 。
path : Path
该属性保存了用于布局项目的路径。
另请参阅 Path 。
pathItemCount : int
该属性保存路径上在任何时刻可见的项目数量。
Qt Quick Ultralite 还利用该属性静态分配委托对象。如果模型中的项目数量少于 `pathItemCount`,则仅使用所需数量的委托实例。
这些属性用于设置视图中高亮项(当前项)的首选范围。首选值必须在0 到1 之间。
注意: 目前尚不支持动画 和触摸手势,因此始终使用默认值PathView.StrictlyEnforceRange 。这意味着,currentItem 位于该范围的起始位置。
定义一个高亮范围,以控制视图移动时当前项最终的位置。例如,若希望当前选中的项位于路径中间,请将高亮范围设置为0.5,0.5 。
注意: 有效的范围 要求preferredHighlightEnd 大于或等于preferredHighlightBegin 。
附加属性文档
PathView.isCurrentItem : bool [read-only]
如果委托对象是当前项,则此附加属性为true ;否则为false 。它附加在委托对象的每个实例上。
当前项是指与 `PathView` 的 `currentIndex ` 属性索引相同的委托项。
该属性可用于调整当前项的外观。
delegate: Item { id: wrapper height: 100 width: 100 required property string icon required property string name required property int index readonly property bool isCurrent: PathView.isCurrentItem opacity: isCurrent ? 1 : 0.5 // Note: to keep the current item on top of the others change the z value z: isCurrent ? 1 : 0 Image { anchors.horizontalCenter: nameText.horizontalCenter width: 64; height: 64 source: wrapper.icon } Text { id: nameText text: wrapper.name font.pointSize: 16 anchors.bottom: parent.bottom } } }
方法文档
void decrementCurrentIndex()
将当前索引减1。
注意: 仅在组件完成之后才调用 此方法。
void incrementCurrentIndex()
将当前索引递增。
注意: 仅在组件完成之后才调用 此方法。
在某些 Qt 许可下可用。
了解更多信息。