首页 > 解决方案 > QML:如何在 TreeView 内的委托中获取 QModelIndex

问题描述

我在 qml 中有一个代表TreeView。我想得到它的QModelIndex. 通过

 model.index

我只得到代表所在的行号。但是,我需要将 a 传递QModelIndex给 c++ 端(制作 aQPersistentModelIndex并将其存储以备后用)。

标签: qtqmlqt5qmodelindex

解决方案


根据文档

itemDelegate : 组件

这个属性定义了一个委托来绘制一个特定的单元格。

在项目委托中,您可以访问以下特殊属性:

styleData.selected - if the item is currently selected
styleData.value - the value or text for this item
styleData.textColor - the default text color for an item
styleData.row - the index of the view row
styleData.column - the index of the view column
styleData.elideMode - the elide mode of the column
styleData.textAlignment - the horizontal text alignment of the column
styleData.pressed - true when the item is pressed
styleData.hasActiveFocus - true when the row has focus
styleData.index - the QModelIndex of the current item in the model
styleData.depth - the depth of the current item in the model
styleData.isExpanded - true when the item is expanded
styleData.hasChildren - true if the model index of the current item has or can have children
styleData.hasSibling - true if the model index of the current item has a sibling

在您的情况下,您必须使用styleData.index.


推荐阅读