Skip to content
  • 0 Votes
    3 Posts
    3k Views
    E

    Sort the tree view by click a column.

    Set the view can be sorted by click the "header".

    treeView_->setSortingEnabled(true);

    Connect the header signal to a slot made by you.

    connect(treeView_->header(), SIGNAL(sortIndicatorChanged(int, Qt::SortOrder)), treeModel_, SLOT(sortByColumn(int, Qt::SortOrder)));

    In the slot, call the sort() virtual function of the model. sort() virtual function is a virtual function of QAbstractItemModel, and one should override it.

    void TreeModel::sortByColumn(int column, Qt::SortOrder order) { sort(column, order); }

    Override the sort() function as your model should do.

    emit dataChanged(QModelIndex(), QModelIndex()); from a model to update the whole tree view.