當我試圖改變包含到一個ListModel的一個項目的屬性值下面的代碼沒有任何影響:QML:的setProperty沒有效果
Main.qml
import QtQuick 2.0
Item {
anchors.fill: parent
ListModel { id: modelCrayon }
Component.onCompleted: {
for (var i = 0; i < 10; i++)
modelCrayon.append({ _tag: i, _source: "resources/crayon-green.png", _selected: false })
}
Column {
x: -170
spacing: 0
Repeater {
model: modelCrayon
delegate: Crayon {
tag: _tag
source: _source
selected: _selected
onCrayonSelected: {
for (var i = 0; i < modelCrayon.count; i++) {
if (i == tag) continue;
modelCrayon.setProperty(i, "_selected", false);
}
}
}
}
}
}
Crayon.qml
import QtQuick 2.0
Image {
property bool selected
property int tag
signal crayonSelected()
id: crayon
smooth: true
fillMode: Image.PreserveAspectFit
onSelectedChanged: console.debug(tag, selected)
MouseArea {
anchors.fill: parent
onClicked: {
selected = !selected
if (selected) crayonSelected()
}
}
states: State {
name: "selected"; when: selected == true
PropertyChanges { target: crayon; x: 30 }
}
transitions: Transition {
from: ""; to: "selected"
PropertyAnimation { property: "x"; duration: 500; easing.type: Easing.InOutQuad }
}
}
控制檯上沒有顯示任何內容,所以「selected」var始終不變。 我確定有一些明顯的我失蹤了。
順便說一下,有沒有一種更智能的方式來使用ListModel作爲OptionBox?我的意思是我只想在時間只有一個項目必須具有選定的屬性== true。或者換句話說,保持所選索引的軌道。
我不知道這是否是「真實」的答案。我會說這是一種解決方法,但我解決了我的問題,在ListView中更改列並將選定的屬性設置爲ListView.isCurrentItem。 – Mark