4
下面我创建一个带委托的ListView,其中包含一个CheckBox,其checked
属性绑定到该模型的checked
角色。单击代理时,我想通过更改模型的checked
属性来切换复选框状态。但checkBox.checked
和model.checked
之间的绑定仅在用户第一次单击该代理时才起作用。之后,总是检查checkBox
,与model.checked
值无关。结果是用户不能取消选中复选框,我不想要这个。更改QML ListView的模型不会更改相应的代理
import QtQuick 2.2
import QtQuick.Controls 1.1
ListView { id: listView
height: 100
width: 100
model: ListModel {
ListElement { checked: false }
ListElement { checked: false }
}
delegate: Rectangle {
width: listView.width
implicitHeight: checkBox.implicitHeight * 1.3
CheckBox { id: checkBox
anchors.fill: parent
text: index + 1
checked: model.checked
}
MouseArea { id: mouseArea
anchors.fill: parent
onClicked: {
var item = listView.model.get(index);
console.log('old model.checked:', model.checked);
item.checked = !item.checked;
console.log('new model.checked:', model.checked);
console.log('checkBox.checked:', checkBox.checked);
console.log('something went wrong:', model.checked !== checkBox.checked);
}
}
}
}
问题在哪里是我的代码,我怎样才能使委托工作像一个正常的CheckBox?
你说得对,我能再现一个最小项目的bug没有任何型号:https://gist.github.com/webmaster128/7979803 –