如何使用QtQuick ListView在onCurrentItemChanged中获取模型
我使用listview的方式如下:如何使用QtQuick ListView在onCurrentItemChanged中获取模型,qt,qml,qquickitem,qquickview,Qt,Qml,Qquickitem,Qquickview,我使用listview的方式如下: ListView { id: list clip: true spacing: 2 anchors.fill: parent model: datas delegate: listItem onCurrentItemChanged: { //I want get the part of the model whi
ListView {
id: list
clip: true
spacing: 2
anchors.fill: parent
model: datas
delegate: listItem
onCurrentItemChanged: {
//I want get the part of the model which belong to currentItem
}
}
Component {
id: listItem
Rectangle {
height: 30
anchors.left: parent.left
anchors.right: parent.right
color: "green"
Label {
anchors.fill: parent
text: uuid
color: "white"
}
MouseArea{
anchors.fill: parent;
onClicked: {
console.log("study clicked")
console.log(uuid)
studyList.currentIndex=index
//component.selected(uuid)
}
}
}
}
我想在
onCurrentItemChanged
中获取属于currentItem
的模型部分
例如:型号
为
ListModel{
ListElement{uuid:aaaa}
ListElement{uuid:bbbb}
ListElement{uuid:cccc}
}
所以应该有3项。如果我单击第二个,我可以获得uuid为bbbb的
ListElement
有什么办法吗?您可以:
onCurrentIndexChanged: {
//I want get the part of the model which belong to currentItem
console.log(datas.get(currentIndex))
}
然后代码将被删除
Item{
width: 800
height: 480
ListModel{
id: datas
ListElement{uuid:"aaaa"}
ListElement{uuid:"bbbb"}
ListElement{uuid:"cccc"}
}
ListView {
id: list
clip: true
spacing: 2
anchors.fill: parent
model: datas
delegate: listItem
onCurrentIndexChanged: {
//I want get the part of the model which belong to currentItem
console.log(datas.get(currentIndex).uuid)
}
}
Component {
id: listItem
Rectangle {
height: 30
anchors.left: parent.left
anchors.right: parent.right
color: "green"
Label {
anchors.fill: parent
text: uuid
color: "white"
}
MouseArea{
anchors.fill: parent;
onClicked: {
list.currentIndex=index
}
}
}
}
}
或者您可以尝试这种方法,当单击按钮时,会发出一个带有参数、当前元素和索引或仅包含元素的信号。
例如:
为什么要在
onCurrentItemChanged
处理程序中获取currentItem
?解决方案是datas.get(currentIndex)
这样您就可以获取我将更新我的solution@sk2212我想把这个项目传递给C++对象.@ BeHTVALID,但是为什么在这个处理程序中?你为什么在这个时候需要这个东西?请告诉我后面的用户故事。@ SK2212,我想:如果我点击列表项,我可以得到属于该项的模型,并将模型传递给C++,并使用该模型的属性来查询DB或在C++中做其他事情。我的实际模型是C++对象继承QAcExcTimeMa模型。因此,只有在声明函数的情况下,我才能通过索引找到项。我只是想知道是否还有其他解决办法。事实上,我不认为使用索引来查找项是一种好方法,我担心如果列表被排序,它会出错。
import QtQuick 2.5
import QtQuick.Window 2.2
import QtQuick.Controls 1.4
Item{
width: 800
height: 480
ListModel{
id: datas
ListElement{uuid:"aaaa"}
ListElement{uuid:"bbbb"}
ListElement{uuid:"cccc"}
}
ListView {
id: list
clip: true
spacing: 2
anchors.fill: parent
model: datas
delegate: listItem
signal sgnElementClicked(var element, var index)
// onCurrentIndexChanged: {
// //I want get the part of the model which belong to currentItem
// console.log(currentItem)
// }
onSgnElementClicked: console.log(element.uuid, index)
}
Component {
id: listItem
Rectangle {
height: 30
anchors.left: parent.left
anchors.right: parent.right
color: "green"
Label {
anchors.fill: parent
text: uuid
color: "white"
}
MouseArea{
anchors.fill: parent;
onClicked: {
// console.log("study clicked")
//console.log(uuid)
list.sgnElementClicked(datas.get(index), index)
/// studyList.currentIndex=index
//component.selected(uuid)
}
}
}
}
}