如何使用 QtQuick ListView 在 onCurrentItemChanged 中获取模型
How to get model in onCurrentItemChanged with QtQuick ListView
我这样使用列表视图:
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
的模型部分
例如:
model
是
ListModel{
ListElement{uuid:aaaa}
ListElement{uuid:bbbb}
ListElement{uuid:cccc}
}
所以应该有 3 项。
如果我点击第二个,我可以得到 ListElement
which uuid is bbbb。
有什么办法吗?
你可以这样做:
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
}
}
}
}
}
或者您可以尝试这种方法,当单击按钮时发出带有参数的信号,当前元素和索引或仅元素。
示例:
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)
}
}
}
}
}
我这样使用列表视图:
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
的模型部分
例如:
model
是
ListModel{
ListElement{uuid:aaaa}
ListElement{uuid:bbbb}
ListElement{uuid:cccc}
}
所以应该有 3 项。
如果我点击第二个,我可以得到 ListElement
which uuid is bbbb。
有什么办法吗?
你可以这样做:
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
}
}
}
}
}
或者您可以尝试这种方法,当单击按钮时发出带有参数的信号,当前元素和索引或仅元素。 示例:
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)
}
}
}
}
}