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

1

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

MouseArea {
   anchors.fill: parent

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

接下来,根据被点击的项目,我有填充不同 QList 项目的函数,这些项目被加载到 ButtonPage.qml 中。

调用的 c++ 中的函数是:

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

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

于 2015-03-03T14:38:24.680 回答