Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

QTableView and QSortFilterProxyModel



  • I am using QTableView, I was originally using just QStnadardItemModel, now I've implemented QSortFilterProxyModel.

    mpsfpTmodel = new QSortFilterProxyModel(this);
    mpsiTmodel = new QStandardItemModel(this);
    mpsfpTmodel->setSourceModel(mpsiTmodel);
    mptvTrecs = new QTableView(this);
    mptvTrecs->setSelectionBehavior(QAbstractItemView::SelectRows);
    mptvTrecs->setMinimumWidth(cintThreatsTableWidth);
    mptvTrecs->setModel(mpsfpTmodel);
    mptvTrecs->setSortingEnabled(true);
    

    The problem I'm having is that the columns contain numbers and these do not sort correctly.
    ddd75ce3-8117-47ac-ba85-a2e0a986b7d4-image.png
    How do I fix this?


  • Moderators

    @SPlatten
    for this you have to subclass.

    The cleaner solution would be to ensure the source data is already in the correct type.

    You could also add a custom ItemRole to your source model and call QSortFilterProxyModel::setSortRole() with that new custom role. In the source model then return the data in the correct type for that role.



  • What's the source of your data in your model and how is your model created? It looks like they are being added to your model as text/strings instead of numbers.



  • @mchinand , the data is read from MariaDB then in the application I process the query and append each record to the model mpsiTmodel. How do I add to the model as numbers?


  • Moderators

    @SPlatten said in QTableView and QSortFilterProxyModel:

    How do I fix this?

    probably they are stored as strings and not integers?!

    Override QSortFilterProxyModel::lessThan():

    bool MySortFilterProxyModel::lessThan(const QModelIndex &source_left, const QModelIndex &source_right) const
    {
         if( source_left.column() == MY_COLUMN && source_right.column() == MY_COLUMN )
             return source_left.data().toInt() < source_right.data().toInt();
    
        return QSortFilterProxyModel::lessThan(source_left,source_right);
    }
    


  • @raven-worx , thank you, is it possible to implement that function in a parent class or is the only way to sub-class QSortFilterProxyModel ?


  • Moderators

    @SPlatten
    for this you have to subclass.

    The cleaner solution would be to ensure the source data is already in the correct type.

    You could also add a custom ItemRole to your source model and call QSortFilterProxyModel::setSortRole() with that new custom role. In the source model then return the data in the correct type for that role.


  • Lifetime Qt Champion

    Hi,

    Out of curiosity, how are you filling your model ?
    If these values are stored as numerical value in your database, you should retrieve and store them in a similar fashion so no need for any kind of subclassing.



  • @SGaist , I've sorted out this problem now, I'm not in front of the laptop I was using, so I will post an update tomorrow when I'm back in front of it.


Log in to reply