QML:如何在 TreeView 中的委托中获取 QModelIndex

QML: How to get the QModelIndex in a delegate inside a TreeView

我在 qml 中有一个委托 TreeView。我想得到它的QModelIndex。通过

 model.index

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

根据 docs:

itemDelegate : Component

This property defines a delegate to draw a specific cell.

In the item delegate you have access to the following special properties:

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