Set model on every PropertyChanges



  • In the example snippet below, when User clicks on button1 the state changes to 'State-A' and the model is set for the ListView (list), displaying the list of 'fruits'. When User clicks on button2 the state changes to 'State-B'. This is causing the ListView to disappear however the count of items in the ListView is still 5.

    Why does this happen?

    Do I need to set the model on every PropertyChanges ?

    import QtQuick 2.3
    import QtQuick.Window 2.2
    import QtQuick.Controls 1.4

    Window {

    visible: true
    width: Math.round(Screen.width/2)
    height: Math.round(Screen.height/2)
    
    Item {
        id: root
        anchors.fill: parent
    
        states: [
            State {
                name: "State-A"
                PropertyChanges {
                    target: list
                    model: fruits
                }
                StateChangeScript {
                    name: "s1"
                    script: {
                        console.log("current state is " + root.state)
                    }
                }
            },
            State {
                name: "State-B"
                StateChangeScript {
                    name: "s2"
                    script: {
                        console.log("current state is " + root.state + "; list count=" + list.count)
                    }
                }
            }
    
        ]
    
        Button {
            id: button1
            anchors.left: root.left
            anchors.top: root.top
            anchors.margins: 10
            text: "Change to State-A"
            onClicked: root.state = "State-A"
        }
    
        Button {
            id: button2
            anchors.left: button1.right
            anchors.top: root.top
            anchors.margins: 10
            text: "Change to State-B"
            onClicked: root.state = "State-B"
        }
    
        ListModel {
            id: fruits
            ListElement { name: "Orange" }
            ListElement { name: "Apple" }
            ListElement { name: "Banana" }
            ListElement { name: "Kiwi" }
            ListElement { name: "Mango" }
        }
    
        Rectangle {
            id: rect
            anchors.centerIn: root
            width: 180
            height: 150
            border.color: "darkblue"
            border.width: 2
            color: "beige"
    
            ListView {
                id: list
                anchors.fill: rect
                anchors.margins: 10
                delegate: Text {
                    text: name
                }
            }
        }
    }
    

    }


Log in to reply
 

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