6

Current behavior

In the picture Test, Test 1 and Test 2 are in the ListView. In this case Test element is highlighted. How can I modify view behavior to ensure that current (highlighted) item stays always in the middle of the list? What I want to achieve is represented in the following picture:

Desired behavior

Jamie Barker
  • 8,145
  • 3
  • 29
  • 64
gmlt.A
  • 327
  • 5
  • 16

2 Answers2

14

You just need highlightRangeMode with preferredHighlightBegin and preferredHighlightEnd. From the documentation:

These properties affect the position of the current item when the list is scrolled. For example, if the currently selected item should stay in the middle of the list when the view is scrolled, set the preferredHighlightBegin and preferredHighlightEnd values to the top and bottom coordinates of where the middle item would be. If the currentItem is changed programmatically, the list will automatically scroll so that the current item is in the middle of the view. Furthermore, the behavior of the current item index will occur whether or not a highlight exists.

Here is a full example of an horizontal ListView with the current item positioned at the center.

import QtQuick 2.4
import QtQuick.Window 2.2

Window {
    width: 300
    height: 150
    visible: true

    ListView {
        anchors.fill: parent
        spacing: 5

        model: 20

        delegate:
            Rectangle {
            width: 30
            color: ListView.view.currentIndex === index ? "red" : "steelblue"

            height: ListView.view.height
            Text {
                anchors.centerIn: parent
                text: index
                font.pixelSize: 20
            }
        }

        orientation: Qt.Horizontal
        preferredHighlightBegin: 150 - 15
        preferredHighlightEnd: 150 + 15
        highlightRangeMode: ListView.StrictlyEnforceRange
    }
}
BaCaRoZzo
  • 7,502
  • 6
  • 51
  • 82
  • I've managed to get desired behavior by using displayMarginEnd/Begin properties and cutting list width, but it seems that your solution is better. Still I've got one problem: on list initiation current item still positioned as illustrated on the first image. I get what I want only if I scroll list at least once. – gmlt.A Apr 17 '15 at 04:12
  • That's THE solution, it's the way API provides what you want. If you can post your code (via pastebin or here) we can try to tackle the problem and see why it does not work for you. – BaCaRoZzo Apr 17 '15 at 06:24
  • Here's my code http://paste2.org/NCOkcDkJ and on this [picture](https://dl.dropboxusercontent.com/u/5324682/list_malfunction.PNG) you can see list initial state. – gmlt.A Apr 17 '15 at 14:58
  • Can you please look what is wrong with my code, @BaCaRoZzo? – gmlt.A Apr 18 '15 at 14:09
  • Nothing actually. :) It is a matter of understanding why such a wrong behaviour occurs. If you set an external variable with a fixed value or set a fixed value for `ListView`, e.g. `width:300 `, it perfectly works. It seems like the highlight window is calculated too early, i.e. when `width` is not set to the final value. I was trying to understand how to work around the issue and if a bug report was available about the (supposed) bug. – BaCaRoZzo Apr 18 '15 at 14:39
0

You could have a look at ListView's positionViewAtIndex method and see if that helps.

BaCaRoZzo
  • 7,502
  • 6
  • 51
  • 82
JediLlama
  • 1,153
  • 8
  • 8