I want to have a QML ListView with a header that stays in place when the items are flicked or scrolled.
To achieve this, I have set the headerPositioning to ListView.OverlayHeader and changed the header's z-order to 2 to keep it on top of the items.
When the list view is displayed, I want the current item to be visible ('scrolled into view').
I have encountered the following issue: The current item is scrolled into view, but the presence of the fixed header is ignored, so the item is partially hidden underneath the header.
Here is my QML:
import QtQuick
import QtQuick.Controls
import QtQml.Models
Window {
id: winMain
width: 640
height: 280
visible: true
title: qsTr("Hello World")
ListModel {
id: components
ListElement { name: "A" }
ListElement { name: "B" }
ListElement { name: "C" }
ListElement { name: "D" }
ListElement { name: "E" }
ListElement { name: "F" }
ListElement { name: "G" }
ListElement { name: "H" }
ListElement { name: "I" }
}
ListView {
id: componentList
x: 10
y: 10
width: parent.width - 20
height: parent.height
spacing: 2
currentIndex: 3
focus: true
clip: true
preferredHighlightBegin: 50
preferredHighlightEnd: height
highlightRangeMode: ListView.ApplyRange
header: Item {
width: parent.width
height: 50
z: 2
Rectangle {
width: parent.width
height: parent.height - componentList.spacing
color: "#000000"
Text {
anchors.centerIn: parent
text: "Components"
color: "white"
font.pointSize: 16
}
}
}
headerPositioning: ListView.OverlayHeader
model: components
delegate: Component {
id: delItem
Rectangle {
width: componentList.width
height: 40
color: ListView.isCurrentItem ? "#FF0000" : "#00FF00"
Text {
text: name
color: "white"
font.pointSize: 14
x: 5
anchors.verticalCenter: parent.verticalCenter
}
MouseArea {
anchors.fill: parent
onClicked: componentList.currentIndex = index
}
}
}
}
}
And this is what it looks like initially:
What could I do to improve this?
Update: Based on the suggestions of smr, I added the three following lines to the original code, but found no difference in the behaviour:
preferredHighlightBegin: 50
preferredHighlightEnd: height
highlightRangeMode: ListView.ApplyRange
I left the lines in the above code sample to show what has been tried so far.

ListViewitems to overlap with the header, why not move the header outside theListView?ListView.OverlayHeadercorrectly: a header that is not subject to scrolling. Is my interpretation wrong?preferredHighlightBegin: ListView.ApplyRange,preferredHighlightBegin: 50, andpreferredHighlightEnd: height, and your ListView will try to keep the selected item below the header.