2
votes

My tree view displays correctly, but when I click the item in the tree I can't get the parent name.

My QML code can be found below.

TreeView {
    id:treeviewID
    anchors.fill: parent
    model: theModel
    style: styleTreeView
    selection: ItemSelectionModel{
        id:sel
        model: theModel
    }
    headerVisible : true
    selectionMode: SelectionMode.SingleSelection

    onDoubleClicked:{

        console.log("parent Name of the Item selected : ",?????)       

    }

    TableViewColumn {
        role: "name_role"
        title: valueSelected
    }
}
1
Could you show us your model?Tarod
I am resolved the probleme, I am add a function in my model c++, when I get the QModelIndex .think you !!aryan
Great! Please, post your own answer and mark it as the right one ;) Happy coding!Tarod

1 Answers

0
votes

I am resolved the problem and this is my solution:

TreeView {
    id:treeviewID
    anchors.fill: parent
    model: theModel
    style: styleTreeView
    selection: ItemSelectionModel{
        id:sel
        model: myModel
    }
    headerVisible : true
    selectionMode: SelectionMode.SingleSelection

    itemDelegate: Rectangle {
            id:r
            height: 20
            color:"transparent"       

            MouseArea{

                anchors.fill: parent
                onClicked:{
                   var indexSelected = styleData.index ;
                   var indexParent   = indexSelected.parent;
console.log("parent name is ",myModel.getParentName(indexParent));

                 }

    TableViewColumn {
        role: "name_role"
        title: valueSelected
    }
}

MyModel is:

TreeViewModel::TreeViewModel(QObject *parent) : QStandardItemModel(parent)
{ }
/**
 * @brief TreeViewModel::getParentName
 * @param itemIndex
 * @return string: name of parent item
 */
QString TreeViewModel::getParentName(const QModelIndex &itemIndex){
    QVariant value1 = itemIndex.data(Qt::DisplayRole);
    if(value1.isValid())
        return  value1.toString();
    else
        return QString();
}