【问题标题】:How to change child property when parent property change in a treeview?如何在树视图中更改父属性时更改子属性?
【发布时间】:2017-09-07 00:05:57
【问题描述】:

我想更改 QML 树视图中已确定父节点的子节点,我想遍历每个子节点并更改我想要的属性,但我不知道如何从父节点获取子节点列表。 我有以下 QML 菜单:

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

当我单击节点时,它会打开一个菜单,单击“显示”按钮后会更改所选节点的属性,我需要从此节点获取其子节点并更改在父节点上更改的相同属性.

我该怎么做?

【问题讨论】:

  • 在模型端实现这一点可能更容易。

标签: c++ qt treeview qml


【解决方案1】:

您可以使用DelegateModel 来获取孩子的QModelIndexes。

DelegateModel
{
  id:delegateModel

  rootIndex: styleData.index
}

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

访问给定QModelIndex 的数据仍然不是那么容易,但已经在this post 中进行了描述。

正如我在评论中所建议的,在 C++ 中的模型端实现这个逻辑可能会更容易。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-10
    • 2017-12-26
    • 2014-01-12
    相关资源
    最近更新 更多