ListView resets delegates that have disappeared from view



  • I have a list with items you can interact with and alter, but as soon as the item has been out of view it is being reset to the "base state". I include an example; clicking a tile will change its color and any tile that didn't have the highlight (i.e. all but the first in this example) will go back to black if you flick them out of view. I want them to retain their altered state. Am I doing something wrong or are ListView's not supposed to be used this way?

    @
    import QtQuick 1.1

    Rectangle {
    width: 360
    height: 360

    ListModel {
        id: model
    
        ListElement {
            name: "element";
        }
        ListElement {
            name: "element";
        }
        ListElement {
            name: "element";
        }
        ListElement {
            name: "element";
        }
        ListElement {
            name: "element";
        }
    }
    
    Component {
        id: delegate
    
        Item {
            id: delegateItem
    
            width: 40
            height: 40
    
            Rectangle {
                id: tile
    
                anchors.fill: parent;
                property int myClick: 0;
                property string myColor: "black";
                color: myColor;
    
                MouseArea {
                    anchors.fill: parent
    
                    onClicked: {
                        if (parent.myClick == 0) {
                            parent.myColor = "red";
                            parent.myClick = 1;
                        }
                        else {
                            parent.myColor = "black";
                            parent.myClick = 0;
                        }
                    }
                }
            }
        }
    }
    
    ListView {
        id: listView
        anchors.fill: parent;
        anchors.margins: 5
        model: model
        delegate: delegate
        spacing: 1
        clip: true
    }
    

    }
    @


  • Moderators

    Hi,

    Welcome to Qt DevNet.
    Please have a look at "this":http://qt-project.org/forums/viewthread/3016 post.



  • It's a feature, not a bug.
    Please don't save state in delegates. Instead, save state in the model itself


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.