how to sort QML TableView in QtQuick 2?

拟墨画扇 提交于 2019-12-03 09:14:00

Add the following code to the QML:

TableView
{
...
    onSortIndicatorColumnChanged: model.sort(sortIndicatorColumn, sortIndicatorOrder)
    onSortIndicatorOrderChanged: model.sort(sortIndicatorColumn, sortIndicatorOrder)
}

Make sure that your C++ model has the accessible sort() method, for example:

class MySortFilterProxyModel : public QSortFilterProxyModel
{
    Q_OBJECT
    ...
    Q_INVOKABLE virtual void sort(int column, Qt::SortOrder order = Qt::AscendingOrder)
    {
        qDebug("Sorting by column %d", column);
        QSortFilterProxyModel::sort(column, order);
    }
}

You can name the method in a different way, I prefer to override the existing method sort().

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!