QML ListView current item not changing with keystrokes or mouse

qml, qt

Solution

The keyboard handling is done automatically:

import QtQuick 2.0
import QtQuick.Controls 1.1

Rectangle {
    width: 400
    height: 400

    ListView {
        id: logListView
        anchors.fill: parent
        model: 10

        delegate: Text {
            text: "Log Item: " + modelData
        }
        highlight: Rectangle { 
            color: "lightsteelblue"; 
            radius: 5
        }
        focus: true
        clip: true
    }
}

If using the up and down arrow keys does not change the selected item for you, using the code above, then it's a bug.

Using a mouse to select items is not handled by default, however; only flicking/dragging of the list is. It's easy to add, though:

import QtQuick 2.0
import QtQuick.Controls 1.1

Rectangle {
    width: 400
    height: 400

    ListView {
        id: logListView
        anchors.fill: parent
        model: 10

        delegate: Text {
            text: "Log Item: " + modelData

            MouseArea {
                anchors.fill: parent
                onClicked: logListView.currentIndex = index
            }
        }
        highlight: Rectangle {
            color: "lightsteelblue";
            radius: 5
        }
        focus: true
        clip: true
    }
}

Problem

I have a very simple ListView. ``` ListView { id: logListView anchors.fill: parent model: LogEntryListModel delegate: Text { text: "Log Item: " + timestamp + ", " + verb } highlight: Rectangle { color: "lightsteelblue"; radius: 5 } focus: true clip: true } ``` It shows the model fine and highlights the first item. It does not move the highlight when I click on another item nor when I use the arrow keys. I know how to control the highlighted item manually by adding event handlers but I see references in the docs to automatic handling of the selectedItem. I was wondering: Does QML provide an automatic changing of the selected item highlighting? What do I need to add to turn it on?

Original source