0

I have a working treemodel, called myModel (derived from QAbstractItemModel) in C++ and I can show it in QML using the QML Treeview control and the TreeViewDelegate

When the mouse is pressed on one treeview item, I want to pass a QModelIndex (or parameters which I can use in C++ for constructing a QModelIndex) back to C++ for further processing.

I can use the model.index but this gives me the index in the view (which corresponds to the selected row), but not a useable QModelIndex. Also I experiemented with the depth of the treeview. But I am stuck.

Has anyone a solution?

BTW: I am using Qt6.4. I found something for Qt5, which is now archived (QML: How to get the QModelIndex in a delegate inside a TreeView).

This is the code where I try to investigate the behaviour of the treeview (the call to _controller(...) is the entry point back to C++:

TreeView {
            id: garminDrives
            Layout.fillHeight: true
            Layout.fillWidth: true
            Layout.preferredHeight: root.height
            Layout.preferredWidth: root.width

            delegate: TreeViewDelegate {

                MouseArea {
                    anchors.fill: parent
                    onClicked: {
                        console.log("Tree item tapped. Model name = " + model.name
                                    + " Index to be passed back to C++ = " + model.index,
                                    " Depth = " + garminDrives.depth(model.index) + " row =  "
                                    + model.row + " col = " + model.column
                                    + " currentRow = " + garminDrives.currentRow
                                    + " currentCol = " + garminDrives.currentCol
                                    )

                        _controller.getSelectedRow(model.index)
                    }
                }

                contentItem: Label {
                    id: delegatetext
                    text: model.name
                }
            }
            model: myModel
        }
Money
  • 107
  • 5

1 Answers1

0

My problem was that row of the TreeViewDelegate was valid, but column was undefined. (If you have multiple columns?)

The workaround I found was this:

TreeView{
    id: treeView
    //...
    delegate: TreeViewDelegate{
        id: item
        //...
        MouseArea{
            //...
            onClicked: function(mouseEvent){
                treeView.modelIndex( treeView.cellAtPosition( item.x + mouseEvent.x, item.y + mouseEvent.y, true ) );               
            }
        }
    }
}
Marschos
  • 33
  • 4
  • Not sure, if it is working. Currently I am having one column. But this will change. My test sample only has a root node with two children. If I click on the root node, I will get the same result as if I click on the first child "QModelIndex(0,0,0x600002ed0118,GarminTreeModel(0x16faaf068))". If I click on the second child, "QModelIndex(1,0,0x600002ed0218,GarminTreeModel(0x16faaf068))" is printed in the console. So, if, only the first parameter differs. Interestingly, if I log index.row to the console, I am getting 0 for the root, 1 and 2 for the child nodes. Anyway, thank you. – Money Mar 31 '23 at 08:42