1
votes

I have a model that works well with QTreeView. In the model I implemented a sort that looks like this:

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);
}

when I call this function, QTreeView updates the view, but TreeView in QML don't do this. QML TreeView usage:

TreeView
{
    model: treeModel
    TableViewColumn 
    {
        title: "Title"
        role: "title"
        width: 700
    }
}

What am I doing wrong? Why the view does not update the layout of the elements after sorting?

1

1 Answers

-1
votes

I think you need to delegate the tree view item. Data is provided to delegate.

Try changing your QML TreeView as shown below by adding itemDelegate

TreeView
{
    model: treeModel

    itemDelegate: Item {
       Text {
               color: styleData.textColor
               text: styleData.value
            }
    }

    TableViewColumn 
    {
        title: "Title"
        role: "title"
        width: 700
    }
}

Look into below link to understand the importance of delegate, between model and QML view. There is an image which easily explains.

http://doc.qt.io/qt-5/qtquick-modelviewsdata-modelview.html

Delegate - dictates how the data should appear in the view. The delegate takes each data in the model and encapsulates it. The data is accessible through the delegate.