【问题标题】:Custom sorting method of QTableView?QTableView的自定义排序方法?
【发布时间】:2012-03-23 12:23:02
【问题描述】:

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

默认排序算法是针对字符串的,我想要一些特定列的数字排序方法。

谢谢。

【问题讨论】:

    标签: qt4 qtableview qt4.7 qstandarditemmodel


    【解决方案1】:

    您应该使用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);
    

    【讨论】:

    • 适用于QTableWidget吗?
    猜你喜欢
    • 2011-04-27
    • 2023-03-11
    • 2014-09-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-27
    • 2016-01-09
    • 1970-01-01
    相关资源
    最近更新 更多