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.
How do I fix this? -
@raven-worx , thank you, is it possible to implement that function in a parent class or is the only way to sub-class QSortFilterProxyModel ?
@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.
-
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.
How do I fix this?@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); }
-
@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 ?
-
@raven-worx , thank you, is it possible to implement that function in a parent class or is the only way to sub-class QSortFilterProxyModel ?
@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.
-
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. -
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.