2

リストビューで QAbstractListModel サブクラスを使用しています。そして、どの項目がクリックされたかに基づいて、C++ を介して QML に渡される新しいリストモデルを生成できるようにしたいと考えています。

リスト内のどのアイテムがクリックされたかを知るために実装する方法は何ですか?

私はウェブをかなり検索しましたが、それを行うための最良の方法を見つけることができないようです.

このコードは、トップレベルとして表示されるグループリストを作成し、グループリストの各項目の下に表示されるネストされたリストを持ちます。ネストされたリストは明らかにグループの子を示しています..そのため、クリックされているオブジェクトにアクセスして、バックエンドで使用して子の新しいリストを生成できるようにする方法が必要です。

コードは次のとおりです。

import QtQuick 2.4
import QtQuick.Window 2.2
//import ListMode 1.0

Rectangle {
    height: 250
    width: 140
    color: "pink"

    //property var aNum: 0

    Component {
        id: folderDelegate

        Item {
            width: 140
            height: col2.childrenRect.height

            Column {
                id: col2
                anchors.left: parent.left
                anchors.right: parent.right


                Rectangle {
                    height: 20
                    width: parent.width
                    border.color: "black"

                    MouseArea {
                        anchors.fill: parent
                        onClicked: console.log(folderlist.contentItem) <<== this is not enough
                    }

                    Text {
                        anchors.horizontalCenter: parent.horizontalCenter
                        anchors.verticalCenter: parent.verticalCenter
                        id: name1
                        text: model.Name
                    }
                }
            }
        }
    }

    ListView {
        id: outer
        model: myModel
        delegate: groupsDelegate
        anchors.fill: parent
    }

    Component {
        id: groupsDelegate

        Item {
            width: 140
            height: col.childrenRect.height

            Column {
                id: col
                anchors.left: parent.left
                anchors.right: parent.right

                Text {
                    anchors.horizontalCenter: parent.horizontalCenter
                    id: t1
                    font.bold: true
                    font.underline: true
                    font.pointSize: 9
                    text: model.Name
                }

                ListView {
                    id: folderlist
                    model: treemodel.lists[treemodel.modIndex]
                    delegate: folderDelegate
                    contentHeight: contentItem.childrenRect.height
                    height: childrenRect.height
                    anchors.left: parent.left
                    anchors.right: parent.right
                    clip: true
                }
            }
        }
    }
}

クリックされたアイテムの名前を取得するだけでなく、実際のオブジェクトを取得できるようにしたいと考えています。新しい子を正しく識別するには、オブジェクトからより多くのデータを抽出する必要があるからです。

皆さんが私を助けてくれたら、とても感謝しています!

前もって感謝します!

4

1 に答える 1