Why onCurrentIndexChanged in ListView isn't executed?



  • Why onCurrentIndexChanged is called only once at startup? When I change 'page' then currentIndex doesn't change. Here is my code:

        ApplicationWindow
        {
            visible: true
    
            ListView
            {
                anchors.fill: parent
    
                orientation: ListView.Horizontal
                snapMode: ListView.SnapOneItem
    
                onCurrentIndexChanged: console.log("currentIndex changed: " + currentIndex)
    
                model: 10
    
                delegate: Rectangle
                {
                    width: ListView.view.width
                    height: ListView.view.height
    
                    color: "#ffffffff";
                    border.color: "#ff000000";
    
                    Text
                    {
                        anchors.centerIn: parent
                        text: index
                    }
                }
            }
        }
    


  • Ok, solution found. currentIndex/onCurrentIndexChanged is useless in this scenario. Here is updated code:

    ApplicationWindow
    {
        visible: true
    
        ListView
        {
            anchors.fill: parent
    
            orientation: ListView.Horizontal
            snapMode: ListView.SnapOneItem
    
            model: 10
    
            onMovementEnded: console.log("current item changed: " + itemAt(contentX,contentY).itemIndex)
    
            delegate: Rectangle
            {
                property int itemIndex: index
    
                width: ListView.view.width
                height: ListView.view.height
    
                color: "#ffffffff";
                border.color: "#ff000000";
    
                Text
                {
                    anchors.centerIn: parent
                    text: index
                }
            }
        }
    }
    

Log in to reply
 

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