Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

ListView currentIndex strange behaviour



  • Hello.
    This is very confusing for me, I don't know why this is working the way it is.
    Let me explain.
    I have ApplicationWindow which contains ListView. I want ListView to be the size of its parent.

    import QtQuick.Controls 1.3
    import QtQuick 2.4
    import QtQuick.Layouts 1.1
    import QtQml.Models 2.1
    
    ApplicationWindow {
        width: 800
        height: 600
        visible: true
        id: window
    
        property alias cwidth: window.width
    
        ListView {
            id: listView
            orientation: ListView.Horizontal
            snapMode: ListView.SnapOneItem
            boundsBehavior: Flickable.StopAtBounds
            highlightMoveDuration: 250
            currentIndex: 1
    
            height: parent.height
    
            // Case #1
            width: parent.width
    
            // Case #2
    //                width: window.width
    
            // Case #3
    //                anchors.fill: parent
    
            model: ObjectModel {
                Page {
                    width: listView.width
                    height: listView.height
                    color: "yellow"
                    text: "Page1"
                }
    
                Page {
                    width: listView.width
                    height: listView.height
                    color: "cyan"
                    text: "Page2"
                }
    
                Page {
                    width: listView.width
                    height: listView.height
                    color: "magenta"
                    text: "Page3"
                }
            }
        }
    }
    
    //Page is simply a Rectangle with a Text
    import QtQuick 2.4
    
    Rectangle {
        smooth: true
        antialiasing: true
    
        property alias text: nameText.text
    
        Text {
            id: nameText
            anchors.centerIn: parent
            text: "PAGE 1"
            font.pointSize: 24
        }
    }
    

    I set listView.currentIndex to 1 so the first thing I am supposed to see, after I launch the application is Page2 with cyan color.

    There are 3 cases.

    • parent.width
    • window.width
    • anchors.fill: parent OR window

    When I set listView.width to parent.width and launch application, I see Page1 instead of Page2 - Not good.
    When I set listView.width to window.width and launch application, I see Page2 - Good.
    When I set anchors.fill to parent and launch application, I see Page1 instead of Page2 - Not good.

    When I print listView.currentIndex property, it says it has value 1(in each case), exactly what I assigned to it.

    Do you have any idea why this happens? I would be very grateful if someone could explain it to me.


Log in to reply