Qt QML-点击时显示菜单的ListView项
我正在寻找一些提示和指针,当用户点击列表项时,在列表项下显示菜单 如果我有这样一个列表模型:Qt QML-点击时显示菜单的ListView项,qt,qml,qt5,qtquick2,qt-quick,Qt,Qml,Qt5,Qtquick2,Qt Quick,我正在寻找一些提示和指针,当用户点击列表项时,在列表项下显示菜单 如果我有这样一个列表模型: ListModel { ListElement { name: "Bill Smith" number: "555 3264" } ListElement { name: "John Brown" number: "555 8426" } ListElement { name: "Sa
ListModel {
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 }
}
}
}
ListView {
anchors.fill: parent
model: ContactModel {}
delegate: contactDelegate
highlight: Rectangle { color: "lightsteelblue"; radius: 5 }
focus: true
}
}
查看其他一些QML示例,我发现一些代码添加了鼠标earea,并根据窗口-菜单高度和宽度定位菜单:
MouseArea {
anchors.fill: parent
onClicked: {
menu.x = (window.width - menu.width) / 2
menu.y = (window.height - menu.height) / 2
menu.open();
}
}
无论我如何努力让它工作,有人能给我指出正确的方向吗?如果确定菜单的父菜单是ListView,那么只需确定通过mapToItem按下的项目的相对位置即可:
完整的示例可以在下面找到。您可以解释想要得到什么,您的解释可以用几种方式解释。我希望菜单位于点击的列表项下方。就在它的正下方。你把MouseArea代码放在哪里了?
MouseArea {
anchors.fill: parent
onClicked: {
menu.x = (window.width - menu.width) / 2
menu.y = (window.height - menu.height) / 2
menu.open();
}
}
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: {
var pos = mapToItem(listView, 0, height)
menu.x = pos.x
menu.y = pos.y
menu.open()
}
}
}
}
ListView {
id: listView
objectName: "list"
anchors.fill: parent
model: ContactModel{}
delegate: contactDelegate
focus: true
Menu {
id: menu
MenuItem { text: "item1" }
MenuItem { text: "item2"; }
MenuItem { text: "item3"; }
}
}
}