I add mouse area to change current index. But it is ugly to hard code view id in mourse area. How can mourse area know delegate's view?
ListModel {
id: contactModel
ListElement {
name: "Bill Smith"
number: "555 3264"
}
ListElement {
name: "John Brown"
number: "555 8426"
}
ListElement {
name: "Sam Wise"
number: "555 0473"
}
}
Rectangle {
width: 180; height: 200
Component {
id: contactDelegate
Item {
width: 180; height: 40
Column {
Text { text: '<b>Name:</b> ' + name }
Text { text: '<b>Number:</b> ' + number }
}
MouseArea{
anchors.fill: parent
onClicked: {
testListView.currentIndex = index [COLOR="#FF0000"]# it is ugly, how can mouse area know view but not hard code view's ID?[/COLOR]
}
}
}
}
ListView {
id: testListView
anchors.fill: parent
model: contactModel
delegate: contactDelegate
highlight: Rectangle { color: "lightsteelblue"; radius: 5 }
focus: true
}
}
ListModel {
id: contactModel
ListElement {
name: "Bill Smith"
number: "555 3264"
}
ListElement {
name: "John Brown"
number: "555 8426"
}
ListElement {
name: "Sam Wise"
number: "555 0473"
}
}
Rectangle {
width: 180; height: 200
Component {
id: contactDelegate
Item {
width: 180; height: 40
Column {
Text { text: '<b>Name:</b> ' + name }
Text { text: '<b>Number:</b> ' + number }
}
MouseArea{
anchors.fill: parent
onClicked: {
testListView.currentIndex = index [COLOR="#FF0000"]# it is ugly, how can mouse area know view but not hard code view's ID?[/COLOR]
}
}
}
}
ListView {
id: testListView
anchors.fill: parent
model: contactModel
delegate: contactDelegate
highlight: Rectangle { color: "lightsteelblue"; radius: 5 }
focus: true
}
}
To copy to clipboard, switch view to plain text mode
Bookmarks