0

I want to change the children of a determined parent node in a QML treeview, I thought to iterate through every child and change the property that I want, but I don't know how to get the children list from a parent. I have the follow QML Menu:

TreeView {
  id: tree
  anchors.fill: parent
  model: model
  itemDelegate: CustomNode{
    id: node
    Menu {
        id: menu
        MenuItem {
            text: "Show"
            onTriggered: {
                styleData.value.active = !!+state
            }
        }
    }

    MouseArea{
        anchors.fill: parent
        acceptedButtons: Qt.LeftButton
        onClicked: {
            idNode = styleData.value.vredId
            menu.popup()
        }
    }
  }
}

When I click on the node it opens a menu that after clicked in the "Show" button change a property of the selected node, from this node I need to get its children and change the same property that was changed on the parent.

How can I do it?

Spes
  • 51
  • 6

1 Answers1

1

You can use a DelegateModel to obtain the QModelIndexes of the children.

DelegateModel
{
  id:delegateModel

  rootIndex: styleData.index
}

delegateModel.count // returns the number of children
delegateModel.modelIndex(i) // returns the model index of the ith element.

Accessing the data for a given QModelIndex is still not that easy, but is already described in this post.

As suggested in my comment, it could be easier to implement this logic on the model side in C++.

Community
  • 1
  • 1
m7913d
  • 10,244
  • 7
  • 28
  • 56