如何使用QAbstractListModel子类作为listmodel知道在QML Listview中单击了哪个项目

时间:2015-02-25 19:32:15

标签: qt listview qml qabstractlistmodel

我在listview中使用了QAbstractListModel子类。我希望能够生成一个新的listmodel,它根据点击的项目通过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
                }
            }
        }
    }
}

而不是只获取被点击项目的名称我希望能够获得实际对象,因为我需要从对象中提取更多数据以正确识别新孩子。

如果你们能帮助我,我会非常感激!

提前致谢!

1 个答案:

答案 0 :(得分:1)

为每个项目设置主模型以拥有自己的唯一ID。因此,当单击某个项目时,我会运行一个函数,该函数根据单击的ID +名称来抓取并存储该项目

MouseArea {
   anchors.fill: parent

   onClicked :{
     treemodel.getObject(model.ID + ":" + model.Name)
     stackView.push(Qt.resolvedUrl("content/ButtonPage.qml"))     
   }
}

接下来,基于单击的项目,我有函数填充加载到ButtonPage.qml中的不同QList项。

调用的c ++函数是:

Q_INVOKABLE void getObject(QString index) {
    clickedItemID = index;
    getClickedItem();
    getFilesByFolder();
}

现在,我不确定这是否是一个好的解决方案。但对我来说这很有效。也许它也适用于其他人。