Qt QML更新ListView中项的属性

Qt QML更新ListView中项的属性,qt,qml,qtquick2,Qt,Qml,Qtquick2,您可以将propItemState绑定到代理之外的内容。或者,您可以添加信号处理程序(),它侦听您的模型(例如;或其他一些类),并在满足条件时更改状态 例如: function updateListItems() { for (var index=0;index < listView.count;index++) { console.log("propItemState: "+listView.contentItem.children[index

您可以将
propItemState
绑定到代理之外的内容。或者,您可以添加信号处理程序(),它侦听您的模型(例如;或其他一些类),并在满足条件时更改状态

例如:

function updateListItems() {
    for (var index=0;index < listView.count;index++) {
        console.log("propItemState: "+listView.contentItem.children[index].propItemState);
        listView.contentItem.children[index].propItemState = 2;
    }
}
项目{
Component.onComplete:{
对于(变量i=0;i<10;i++){
append({“myTxt”:“SomeThing”+i});
}
internal.state=“1”;
}
列表模型{
id:myModel
}
QtObject{
id:内部
属性字符串状态:“0”
}
列表视图{
id:listView
宽度:parent.width
高度:parent.height
型号:myModel
代表:矩形{
属性字符串propItemState:internal.state
MyListItem{
id:测试
itemText:myText
项目状态:丙酸状态;
}
}
}
}
From:“根据需要实例化委托,并可能随时销毁。[…]状态不应存储在委托中。”将状态存储在委托外部、模型中或其他位置。
function updateListItems() {
    for (var index=0;index < listView.count;index++) {
        console.log("propItemState: "+listView.contentItem.children[index].propItemState);
        listView.contentItem.children[index].propItemState = 2;
    }
}
Item {
    Component.onComplete: {
        for (var i=0;i < 10;i++) {
            myModel.append({"myTxt": "SomeThing"+i});
        }

        internal.state = "1";
    }

    ListModel {
        id: myModel
    }

    QtObject {
        id: internal
        property string state: "0"
    }

    ListView {
        id: listView
        width: parent.width
        height: parent.height
        model: myModel
        delegate: Rectangle {
            property string propItemState: internal.state

            MyListItem {
                id: test
                itemText: myText
                itemState: propItemState;
            }
        }
    }
}