Solved How to sort original data structure of QAbstractTableModel?
-
@CuriousPan said in How to sort original data structure of QAbstractTableModel?:
bool ForecastingProxyModel::lessThan(const QModelIndex &sourceLeft, const QModelIndex &sourceRight) const
{
const QVariant &leftData = sourceModel()->data(sourceLeft);
const QVariant &rightData = sourceModel()->data(sourceRight);
//sourceModel()->sort(sourceLeft.column(), sortOrder()); you said it's a bad idea to call it hear.if (leftData.userType() == QMetaType::QDate) { return leftData.toDate() < rightData.toDate(); } else if (leftData.userType() == QMetaType::QString) { return leftData.toString() < rightData.toString(); } else if (leftData.userType() == QMetaType::Double) { return leftData.toDouble() < rightData.toDouble(); } else if (leftData.userType() == QMetaType::Bool) { return leftData.toBool() < rightData.toBool(); } return QSortFilterProxyModel::lessThan(sourceLeft, sourceRight);
This is not needed. QSortFilterProxyModel does exact the same (and honors more meta types than your implementation)
-
@Christian-Ehrlicher, do you suggest totally removing the method?
-
@CuriousPan
If you followed my link it lists:This function is used as the < operator when sorting, and handles the following QVariant types:
So you see how it says which
QVariant
types it handles and that includes the ones you handle. It also includesQMetaType::QDate
so I don't know why your "lessThan
method considers Date as String, which I find quite suprising". I don't believe you need your own override.EDIT I can see @Christian-Ehrlicher agrees.
Put
qDebug()
statements inside yoursort()
to see whether it's called. Your view is attached to theQSortFilterProxyModel
rather than the underlying model. I'm not sure, but find out which models' sorting are getting used/called. I don't thinkQSortFilterProxyModel::sort()
will callsourceModel->sort()
. I think it's confusing to have sorting on your source model and also to be usingQSortFilterProxyModel
against it.Where am I supposed to call it?
I would normally expect you to sort a (source) model immediately after you populate it if you want it sorted. So wherever that is in your code.
-
After removing
lessThan
method I have following behavior:QSortFilterProxyModel::sort()
is getting called, but not the source one, hence I have to callsource::sort()
inside it. It sorts source data, but view isn't changing.
-
@CuriousPan
I have said before: I do not know why you are using aQSortFilterProxyModel
and also doingsourceModel()->sort()
. If you want to sort the underlying model I don't know why you want the proxy too. But there you are.Meanwhile, since again you do not show your code for "hence I have to call source::sort() inside it", we do not know what you are doing. Are you/do you need to take
QSortFilterProxyModel::sortColumn()
&QSortFilterProxyModel::sortOrder()
into account?It sorts source data, but view isn't changing.
I have said before: the view is bound to the
QSortFilterProxyModel
, not the source model directly, so I don't know that any change you might or might not make in the source model's rows' order would be reflected in either theQSortFilterProxyModel
or any view bound to it.I would get it working with either the
QSortFilterProxyModel
and no source modelsort()
or source modelsort()
and noQSortFilterProxyModel
--- and possibly compare the two --- before I tried to understand what was happening with both in play.That's all I know to say.
-
After searching the internet I guess I found a solution. After sorting the data it's important to emit
dataChanged
.
Thanks everyone who tried to help me. -
@CuriousPan
That is a good point, which I should have thought about! :)I still don't know why you want both to sort the model and to have a sort proxy on top of it. You sure you want the
QSortFilterProxyModel
at all, given your desire to sort the underlying model? Best of luck. -
@JonB said in How to sort original data structure of QAbstractTableModel?:
I still don't know why you want both to sort the model and to have a sort proxy on top of it.
Because he does not understand what a QSortFilterProxyModel is doing...
-
@Christian-Ehrlicher, yeah, you are right. I was given the example with
QSortFilterProxyModel
so I thought it's mandatory to use it. I've removed it. -
Sorting the base model is normally much more expansive than using a QSortFilterProxyModel so it should be avoided.