PDA

View Full Version : get mouse coordinates in ListView



joko
11th February 2015, 17:00
Hi Qt Masters,

I'm having problem displaying a rectangle when pressing and holding on selected delegate item.



Item {
id: listItem

anchors {
top: oparent.top
left: parent.left
right: parent.right
bottom: parent.bottom
}

ListView {
id: list

anchors.fill: parent

currentIndex: -1
clip: true

model: itemModel
delegate: ItemDelegate {
MouseArea {
id: area
anchors.fill: parent
onPressAndHold: {
menu.showed = true
menu.x = mouseX
menu.y = mouseY
}
}
}
boundsBehavior: Flickable.StopAtBounds

CustomMenu {
id: menu

z: 100

parent: list

model: menuModel
}
}
}


In the code above, it returns only the mouse coordinates on selected item that is why the menu always showed on top part of the Item.
When I tried putting mousearea on listItem, the ListView will not work.

Please advice, thanks.

wysota
11th February 2015, 18:19
You need to put the mouse area on the list view and also make sure all mouse events are propagated from that mouse area to the view by ignoring incoming events in the mouse area element.

joko
12th February 2015, 14:12
You need to put the mouse area on the list view and also make sure all mouse events are propagated from that mouse area to the view by ignoring incoming events in the mouse area element.

Thank you for your response.

However, what I did was put the MouseArea on delegate item then send a signal from onPressAndHold with mouseX and mouseY parameters to the ListView.
Then used mapFromItem on the listItem.



Item {
id: listItem

anchors {
top: parent.top
left: parent.left
right: parent.right
bottom: parent.bottom
}

ListView {
id: list

anchors.fill: parent

currentIndex: -1
clip: true

model: itemModel
delegate: ItemDelegate {
onMenuPosition: {
var pos = listItem.mapFromItem(list.currentItem, mouseX, mouseY)
menu.x = pos.x
menu.y = pos.y
}
}
boundsBehavior: Flickable.StopAtBounds

CustomMenu {
id: menu

z: 100

parent: list

model: menuModel
}
}
}

wysota
12th February 2015, 15:35
That's much slower and definitely breaks encapsulation.