qt 如何从listview中获取选中的数据

如标题
2025-05-01 18:29:52
推荐回答(1个)
回答1:

import QtQuick 2.1
import QtQuick.Window 2.1
import QtQuick.Controls 1.2
Window {
visible: true
width: 360
height: 360

Rectangle {
width: 200; height: 200

ListModel {
id: fruitModel
property string language: "en"
ListElement {
name: "Apple"
cost: 2.45
}
ListElement {
name: "Orange"
cost: 3.25
}
ListElement {
name: "Banana"
cost: 1.95
}
}

ListModel {
id: fruitModel2
property string language: "en"
ListElement {
name: "A"
cost: 2.45
}
ListElement {
name: "B"
cost: 3.25
}
ListElement {
name: "C"
cost: 1.95
}
ListElement {
name: "D"
cost: 1.95
}
}

Component {
id: fruitDelegate
Item{
Row {
Label{
objectName: "lblName" + index + "1"
text: " Fruit: " + name
}
Label{
objectName: "lblName" + index + "2"
text: " Fruit: " + name
}
}
}

}

ListView {
id:list
property color fruit_color: "green"
model: fruitModel
delegate: fruitDelegate
anchors.fill: parent
}

Rectangle {
x:200
width: 50; height: 50
color: "red"
MouseArea{
anchors.fill: parent
onClicked: {
list.model = fruitModel2
console.log(list.children[0].children[3].children[0].children[0].objectName)
console.log(list.children[0].children[2].children[0].children[1].objectName)
}
}
}
}
}