Search code examples
qtqmlqquickitemqquickview

How to get model in onCurrentItemChanged with QtQuick ListView


I use the listview like this:

    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)
                }
            }

       }
}


I want get the part of the model which belong to currentItem in onCurrentItemChanged.
For example:
the model is

ListModel{
    ListElement{uuid:aaaa}
    ListElement{uuid:bbbb}
    ListElement{uuid:cccc}
}

So there should be 3 item.
If I click the second one, I can get the ListElement which uuid is bbbb.

Is there any way?


Solution

  • You can do:

    onCurrentIndexChanged: {
                        //I want get the part of the model which belong to currentItem
                      console.log(datas.get(currentIndex))
                    }
    

    then the code will be

    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
                    }
                }
    
            }
    
        }
    }
    

    or you can try this approach, when button clicked emit a signal with parameters, the current element and the index or only the element . example:

    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)
                    }
                }
            }
        }
    }