我的树状视图显示正确,但是当我点击树中的项目时,我无法获得父名称。
我的QML代码可以在下面找到。
<Button Height="23" Margin="38,63,0,0" Name="button1"
IsEnabled="{Binding RelativeSource={x:Static RelativeSource.Self}, Path=(Validation.Errors).Count}" />
答案 0 :(得分:0)
我解决了问题,这是我的解决方案:
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是:
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();
}