qtqmlqt5qmodelindex

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


I have a delegate in a qml TreeView. I would like to get its QModelIndex. Vía

 model.index

I only get the number of the row, where the delegate is. However, I need to pass a QModelIndex to the c++ side (to make a QPersistentModelIndex and store it for later use).


Solution

  • According to the 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
    

    In your case you must use styleData.index.