QML TreeView是否支持从模型发出的信号layoutChanged?
问题描述:
我有一个适用于QTreeView的模型。在这个模型中,我实现一种看起来像这样:QML TreeView是否支持从模型发出的信号layoutChanged?
void SimpleTreeModel::sort(Node* sortedNode)
{
emit layoutAboutToBeChanged(QList<QPersistentModelIndex>(), VerticalSortHint);
QModelIndexList oldIndices = persistentIndexList();
Node::SortType sortType = Node::Down;
//sort starting node
sortedNode->sortChildren(sortType);
QModelIndexList newIndices;
newIndices.reserve(oldIndices.size());
for(const auto &i : oldIndices)
{
Node* node = const_cast<Node*>(nodeFromIndex(i));
QModelIndex index = indexFromNode(node);
newIndices.push_back(index);
}
changePersistentIndexList(oldIndices, newIndices);
QModelIndex startingIndex = indexFromNode(sortedNode);
emit layoutChanged({ QPersistentModelIndex(startingIndex) }, VerticalSortHint);
}
当我调用此函数,QTreeView则更新视图,但在TreeView的QML不这样做。 QML TreeView的用法:
TreeView
{
model: treeModel
TableViewColumn
{
title: "Title"
role: "title"
width: 700
}
}
我在做什么错?为什么视图在排序后不更新元素的布局?
答
我认为你需要委托树视图项目。数据提供给委托。
试着改变你的QML 的TreeView通过添加itemDelegate
TreeView
{
model: treeModel
itemDelegate: Item {
Text {
color: styleData.textColor
text: styleData.value
}
}
TableViewColumn
{
title: "Title"
role: "title"
width: 700
}
}
查找到下面的链接,了解代表的重要性,模型和QML视图之间如下图所示。有一个很容易解释的图像。
http://doc.qt.io/qt-5/qtquick-modelviewsdata-modelview.html
代表 - 决定如何将数据应该出现在视图中。代理接收模型中的每个数据并封装它。数据是 可通过委托人访问。
感谢您的回答。但我不明白这应该如何帮助。问题是TreeView不响应来自模型的信号。在阅读了TreeView的源代码之后,我没有在其中找到负责响应模型信号layoutChanged的函数。它让我困惑。 –
@ strelok.ndv查看此链接,您将看到一个图像以及如何委托以在QML视图和模型之间进行通信。http://doc.qt.io/qt-5/qtquick-modelviewsdata-modelview.html – Naidu
更新了答案。 – Naidu