QTableView的自定义排序方法? [英] Custom sorting method of QTableView?

查看:175
本文介绍了QTableView的自定义排序方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何为QTableView或模型设置自定义排序方法? (我应该重新实现哪个功能)

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.

谢谢.

推荐答案

您应使用 QSortFilterProxyModel .您应该重新实现lessThan方法.然后,必须为代理模型设置sourceModel,并将代理模型设置为视图的模型

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

这篇关于QTableView的自定义排序方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆