繁体   English   中英

QML更新ListView中Item的属性

[英]QML update the property of Item inside the ListView

Item {

Component.onComplete: {
    for (var i=0;i < 10;i++) {
        myModel.append({"myTxt": "SomeThing"+i});
    }
}

ListModel {
    id: myModel
}

ListView {
    id: listView
    width: parent.width
    height: parent.height
    model: myModel
    delegate: Rectangle {
        property string propItemState: 0;
        
        MyListItem {
            id: test
            itemText: myText
            itemState: propItemState;
        }
    }
}
}

我想在显示列表后更新propItemState
为此,我尝试了以下方法,但遇到undefined错误。
列表模型更新后,我将调用此方法。

function updateListItems() {
    for (var index=0;index < listView.count;index++) {
        console.log("propItemState: "+listView.contentItem.children[index].propItemState);
        listView.contentItem.children[index].propItemState = 2;
    }
}

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

例子:

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;
            }
        }
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM