我正在尝试使用 Pathview 来显示轮播,当模型中的某个元素将某个字段设置为 true 时,我想在轮播中的项目旁边显示图像。
我尝试模拟一些代码来显示我正在尝试执行的操作,如果 currentIndex 处的项目具有 favorite=true,则 star.jpg 将显示。
property var currentItem: ListModel.get(mypathView.currentIndex)
ListModel {
id:sampleModel
ListElement {
name: "first"
favorite: false
}
ListElement {
name: "second"
favorite: true
}
ListElement {
name: "third"
favorite: false
}
ListElement {
name: "fourth"
favorite: false
}
}
Component {
id: myDelegate
Item {
width: vpx(300)
height: vpx(150)
scale: PathView.iconScale
opacity: PathView.iconOpacity
z: PathView.iconOrder
Rectangle {
anchors.fill: parent
color: "transparent"
Image {
id:listicon
anchors.fill: parent
source: "square.jpg"
}
}
Rectangle {
anchors.fill: parent
color: "transparent"
visible: currentItem.favorite==true
Image {
id:favicon
anchors.left: listicon
source: "star.jpg"
}
}
}
}
PathView {
id: mypathView
anchors.fill: parent
anchors.bottomMargin: 10
anchors.topMargin: 10
pathItemCount: 7
preferredHighlightBegin: 0.5
preferredHighlightEnd: 0.5
highlightRangeMode: PathView.StrictlyEnforceRange
model: sampleModel
delegate: myDelegate
path: Path {
startX: 0; startY: 0
PathAttribute { name: "iconScale"; value: 0.2 }
PathAttribute { name: "iconOpacity"; value: 10.2 }
PathAttribute { name: "iconOrder"; value: 0 }
PathLine {x: mypathView.width / 2; y: mypathView.height/2 }
PathAttribute { name: "iconScale"; value: 1.5 }
PathAttribute { name: "iconOpacity"; value: 1 }
PathAttribute { name: "iconOrder"; value: 8 }
PathLine {x: 0; y: mypathView.height}
}
当您尝试从委托访问模型项属性
favorite
时,似乎出现错误。
我记得委托可以访问所有模型属性。将当前 PathView 项存储在单独的变量中是没有意义的(在没有根 ID 的情况下调用它也是不好的,并且
PathView
已经具有属性 currentItem)。
直接从委托调用
favorite
(根据模型定义,语法应更改)。您可以通过根据需要强制模型属性来确保结果。下面是您的代表的工作示例
Component {
id: myDelegate
Item {
required property var model
width: 300
height: 150
scale: PathView.iconScale
opacity: PathView.iconOpacity
z: PathView.iconOrder
Rectangle {
anchors.fill: parent
color: "indianred"
}
Rectangle {
visible: model.favorite === true
width: 15
height: 15
anchors {
right: parent.right
bottom: parent.bottom
margins: 5
}
color: "gold"
}
}
}