Thursday, August 1, 2013

qml example: Detect mouse click on ListView

Last qml example of ListView response for keyboard only. It is modified in this example to handle mouse click, with MouseArea.

qml example: Detect mouse click on ListView
qml example: Detect mouse click on ListView


import QtQuick 2.0
import QtWebKit 3.0

Rectangle {
    width: 300
    height: 360

    ListView{
        id: myListView
        width: parent.width
        height: 100
        highlight: Rectangle { color: "lightsteelblue"; radius: 5 }
        focus: true
        model: ["Sunday", "Monday", "Tuesday", "Wednesday",
            "Thursday", "Friday", "Saturday"]

        delegate: Component{
            Item{
                property variant itemData: model.modelData
                width: parent.width
                height: 12
                Column{
                    Text {text: model.modelData}
                }
                MouseArea{
                    id: itemMouseArea
                    anchors.fill: parent
                    onClicked: {
                        myListView.currentIndex = index

                    }
                }
            }
        }

        onCurrentItemChanged: myText.text = myListView.currentItem.itemData

    }

    Text {
        id: myText
        anchors.centerIn: parent
    }
}