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

ListView ignores `displayMarginBeginning: 0`?



  • I initialize ListView.contentY to initially hide the header using contentY: 0 so that you only see it if you swipe down, like in iOS Mail. But ListView seams to ignore displayMarginBeginning: 0 and scrolls to reveal the header when currentIndex is set to 0 even when the first item delegate is completely in view.

    Is there a way to prevent this?

    import QtQuick 2.5
    import QtQuick.Layouts 1.12
    
    ListView {
        id: root
        header: Rectangle {  color: 'red'; height: 40; width: parent.width }
        displayMarginBeginning: 0
        delegate: Item {
            width: parent.width
            height: 50
            RowLayout {
                anchors.fill: parent
                Text { text: name }
                Text { text: cost }
            }
            Rectangle {
                color: index == root.currentIndex ? '#aa0000ff' : 'transparent'
                anchors.fill: parent
            }
        }
    
        // will scroll to ensure item is visible, but                                                                                                                                                               
        // ignores displayMarginBeginning for first item                                                                                                                                                            
        Timer {
    	running: true;
            interval: 1000;
    	repeat: true
            onTriggered: {
                if(currentIndex == 0)
    	        currentIndex = 5;
                else
                    currentIndex = 0;
            }
        }
    
        model: ListModel {
            ListElement { name: "Apple"; cost: 2.45 }
            ListElement { name: "Orange"; cost: 3.25 }
            ListElement { name: "Banana"; cost: 1.95 }
            ListElement { name: "Apple"; cost: 2.45 }
            ListElement { name: "Orange"; cost: 3.25 }
            ListElement { name: "Banana"; cost: 1.95 }
        }
    }
    

Log in to reply