簡體   English   中英

如何使用QtQuick ListView在onCurrentItemChanged中獲取模型

[英]How to get model in onCurrentItemChanged with QtQuick ListView

我這樣使用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)
                }
            }

       }
}


我想在onCurrentItemChangedonCurrentItemChanged屬於currentItem的模型部分。
例如:
model

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

        }

    }
}

或者您可以嘗試這種方法,當單擊按鈕時會發出帶有參數,當前元素和索引或僅包含element的信號。 例:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM