簡體   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