如何为QTableView或模型设置自定义排序方法? (我应该重新实现哪个功能)
默认排序算法适用于字符串,我想要一些特定列的数字排序方法。
感谢。
答案 0 :(得分:4)
您应该使用QSortFilterProxyModel。你应该重新实现lessThan方法。然后,您必须为代理模型设置sourceModel,并将代理模型设置为视图的模型
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);