0
我有一個適用於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
}
}
我在做什麼錯?爲什麼視圖在排序後不更新元素的佈局?
感謝您的回答。但我不明白這應該如何幫助。問題是TreeView不響應來自模型的信號。在閱讀了TreeView的源代碼之後,我沒有在其中找到負責響應模型信號layoutChanged的函數。它讓我困惑。 –
@ strelok.ndv查看此鏈接,您將看到一個圖像以及如何委託以在QML視圖和模型之間進行通信。http://doc.qt.io/qt-5/qtquick-modelviewsdata-modelview.html – Naidu
更新了答案。 – Naidu