1

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?

behtgod
  • 251
  • 3
  • 15
  • 1
    Why do you want to get the `currentItem` within `onCurrentItemChanged` handler? – sk2212 Nov 27 '15 at 08:55
  • the solution is `datas.get(currentIndex)` like that you can get the currentItem I'll update my solution – Mido Nov 27 '15 at 20:01
  • @sk2212 I want to pass the item to c++ object. – behtgod Nov 30 '15 at 09:04
  • @behtgod but why within this handler? Why did you need the item at this point? Please tell me the user story behind. – sk2212 Nov 30 '15 at 09:22
  • @sk2212 I want this: If I click the item of list, I can get the model which belong to the item ,and pass the model to c++ and use the model's properties to query DB or do other things in c++. My actual model is c++ object inherits QAbstractItemModel. So I can't find the item by index only if that I declare a function. I just want to know whether there is some other solutions. In fact ,I don't think use index to find item is a good way, I worry about it's wrong if the list is sorted. – behtgod Nov 30 '15 at 09:53
  • @behtgod So you can simply invoke the C++ logic within the `onClicked` handler of the `MouseArea` component. You did not need to handle it in `onCurrentItemChanged` – sk2212 Nov 30 '15 at 10:03
  • @sk2212 You're right. It's a good idea.Thank you. Also thank you ,@Mido – behtgod Dec 01 '15 at 06:00

1 Answers1

3

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)
                }
            }
        }
    }
}
Mido
  • 1,092
  • 1
  • 9
  • 14