1

我像这样使用列表视图:

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

       }
}


我想得到属于currentItemin的模型部分onCurrentItemChanged
例如:
model

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

所以应该有3个项目。
如果我点击第二个,我可以得到ListElement哪个 uuid 是 bbbb。

有什么办法吗?

4

1 回答 1

3

你可以做:

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)
                }
            }
        }
    }
}
于 2015-11-26T20:21:08.537 回答