Custom sorting method of QTableView?

落爺英雄遲暮 提交于 2019-12-11 09:28:48

问题


How can I setup custom sorting method for a QTableView , or for the model ? (Which function should I re-implement)

The default sorting algorithm are for strings , I want a number sorting method for some specific column.

Thanks.


回答1:


You should use QSortFilterProxyModel. You should reimplement lessThan method. Then you have to set sourceModel for your proxy model, and set your proxy model as model for your view

class MyProxyModel: public QSortFilterProxyModel
{
protected:
     bool   lessThan ( const QModelIndex & left, const QModelIndex & right ) const
     {
         // your sorting rules
     }
};

// ... somewhere where your view is accessible
MyProxyModel * m = new MyProxyModel();
m->setSourceModel(yourModel);
yourView->setModel(m);


来源:https://stackoverflow.com/questions/9839116/custom-sorting-method-of-qtableview

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