获取所选项目的QML模型

问题描述

我有一个TreeView,其中填充了QStandardItemmodel项

    QC1.TreeView{
         id: myTreeView
         model: myModel

         selection: ItemSelectionModel{
            id: mySelectionModel
            model: myModel
         }

         itemDelegate: ItemDelegate{
            MouseArea{
                 anchors.fill: parent
                 onClicked: {
                        console.log(styleData.index,model)
                 }
            }
            Text {
               id: name
               anchors.verticalCenter: parent.verticalCenter
               renderType: Text.NativeRendering
            }
         }
         QC1.TableViewColumn {
            title: "Name"
            role: "name"
         }
    }

    Button{
       text: "Select third element"
       onClicked:{
          myTreeView.selection.select(myModel.getIndex(2,0),ItemSelectionModel.ClearandSelect) //custom function,returns index(row,col)
          myTreeView.selection.setCurrentIndex(myModel.getIndex(2,ItemSelectionModel.SelectCurrent)
          //selection model data?
       }
    }

当我单击任何字段时,我就能获得此项目QModelIndexQQmlDMAbstractItemmodelData。单击按钮会自动将所选元素设置为模型中的第三个元素。 myTreeView.selection.currentIndex将给我QModelIndex的选定项目。如何获得QQmlDMAbstractItemmodelData的选定项目?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)