2
votes

i am using a QAbstractListModel subclass in a listview. And i want to be able to generate a new listmodel that is passed to QML through c++ based on which item was clicked.

what is the method to implement to able to tell which item was clicked in the list?

i have searched the web quite a bit but i cannot seem to find the best way to do it.

this code creates a grouplist that is shown as toplevel and it has a nested list that show beneath each item in the grouplist. The nested list obviously shows the children of the group.. so i need a way to get to the object that is being clicked so that i can use it in the back-end to generate new lists of its children.

the code is:

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

instead of only getting the name of the item being clicked i would like to be able to get the actual object because i need to extract more data from the object to correctly identify the new children.

if you guys could help me out, i would appreciate it very much!

Thanks in advance!

1
Since QAbstractListModel doesn't have this feature, you'd have to write it yourself in your subclass.MrEricSir
@MrEricSir thank you for answering. So you are saying i would have to write a function that can be called when the item is clicked. For example a function that stores the object that was retrieved by QAbstractmodel::data() when the item in the listview is clicked. And then using that stored pointer to create new lists. would that be something?CantThinkOfAnything
You could just make a slot in your subclass that returns the item for a given index value. That way you can do whatever you want with the item, either in C++ or QML.MrEricSir
@MrEricSir i see,, im trying to figure out how i will know which model the index is from. The index that is sent to the slot does not say from which model it is. With only an index, it will not be enough... im gonna have to figure that out. After all, my main model is a treemodel that contains sub-listmodelsCantThinkOfAnything

1 Answers

1
votes

the main model is setup for each item to have its own unique ID. So when an item is clicked, i run a function that grabs and stores the item based on the ID + name that was clicked

MouseArea {
   anchors.fill: parent

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

next, based on the item that was clicked i have functions that fill different QList items which are loaded into the ButtonPage.qml.

the function in c++ that is invoked is:

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

now, i am not sure if this is a good solution. But for me it works. Maybe it will work for someone else too.