我在列表视图中使用 QAbstractListModel 子类。而且我希望能够生成一个新的列表模型,该列表模型根据单击的项目通过 c++ 传递给 QML。
实现什么方法可以判断在列表中单击了哪个项目?
我已经在网上搜索了很多,但我似乎找不到最好的方法。
此代码创建一个显示为顶层的组列表,它有一个嵌套列表,显示在组列表中的每个项目下方。嵌套列表显然显示了该组的子级.. 所以我需要一种方法来获取正在单击的对象,以便我可以在后端使用它来生成其子级的新列表。
代码是:
import QtQuick 2.4
import QtQuick.Window 2.2
//import ListMode 1.0
Rectangle {
height: 250
width: 140
color: "pink"
//property var aNum: 0
Component {
id: folderDelegate
Item {
width: 140
height: col2.childrenRect.height
Column {
id: col2
anchors.left: parent.left
anchors.right: parent.right
Rectangle {
height: 20
width: parent.width
border.color: "black"
MouseArea {
anchors.fill: parent
onClicked: console.log(folderlist.contentItem) <<== this is not enough
}
Text {
anchors.horizontalCenter: parent.horizontalCenter
anchors.verticalCenter: parent.verticalCenter
id: name1
text: model.Name
}
}
}
}
}
ListView {
id: outer
model: myModel
delegate: groupsDelegate
anchors.fill: parent
}
Component {
id: groupsDelegate
Item {
width: 140
height: col.childrenRect.height
Column {
id: col
anchors.left: parent.left
anchors.right: parent.right
Text {
anchors.horizontalCenter: parent.horizontalCenter
id: t1
font.bold: true
font.underline: true
font.pointSize: 9
text: model.Name
}
ListView {
id: folderlist
model: treemodel.lists[treemodel.modIndex]
delegate: folderDelegate
contentHeight: contentItem.childrenRect.height
height: childrenRect.height
anchors.left: parent.left
anchors.right: parent.right
clip: true
}
}
}
}
}
我希望能够获取实际对象,而不是仅获取被单击项目的名称,因为我需要从对象中提取更多数据以正确识别新子项。
如果你们能帮助我,我将不胜感激!
提前致谢!