Solved QSortFilterProxyModel, QAbstractItemModel
-
@Slawomir_Piernikowski said in QSortFilterProxyModel, QAbstractItemModel:
So the order of the data in the view is different than in the model.
Which model are you talking about? The
QSortFilterProxyModel
or the underlying model it is a proxy for? (BTW,QTableView
does not do its own sorting, if that's what you think it does, it passes the sorting to its model.) -
Which model are you talking about?:
Castomized QAbstractTableModelFor sorting in this case is responsible QSortFilterProxyModel::Sort function I only trigger this function by calling SetData function of The QAbstractTableModel or by clicking an arrow on header column of the TableView which is responsible for calling also QSortFilterProxyModel::Sort function
So the order of the data in the Tableview is different than in the model(QAbstractTableModel).
-
@Slawomir_Piernikowski said in QSortFilterProxyModel, QAbstractItemModel:
So the order of the data in the Tableview is different than in the model
Correct, that's what
QSortFilterProxyModel
does. It's 50% of its job (the remaining being the filtering part) -
@VRonin You said : Correct, that's what QSortFilterProxyModel does. It's 50% of its job (the remaining being the filtering part)
If QSortFilterProxyModel::Sort function sorts data which I see in the TableView Why my
model(customized QAbsrtractTableModel) data are not sorted???? -
That's exactly what
QSortFilterProxyModel
model do. It reorders the indexes and maintain a mapping between its indexes and those of the source model.
You can imagine it as a telephone switchboard but operating on the model indexes -
@VRonin ok but I need to have model data reorded the same way as the data are sorted and seen in the view
-
Then you have to ditch the
QSortFilterProxyModel
and reimplementsort
in your customQAbstractItemModel
emittinglayoutAboutToBeChanged
andlayoutChanged
and callingchangePersistentIndexes
-
@VRonin I need QSortFilterProxyModel for I use it also for filtring data of the model.
I also need to have consitency of the model(QAbstractTableModel) data and the data after sorting using QSortFilterProxyModel::Sort function.Maybe mapToSource and mapFormSource functions will be helpfull??????
-
@Slawomir_Piernikowski
For the record, we're not sure why you want the underlying data model to have the same sort order as the in the proxy model?Data doesn't really have a sort order, sorting is just one way of viewing the data. At different times you might sort by different criteria. The proxy model also provides filtering for the view, but you don't say you want rows omitted by the filter to be removed from the underlying model, which would be the equivalent change to what you are asking about sorting?
-
@JonB You wrote: we're not sure why you want the underlying data model to have the same sort order as the in the proxy model?
For during setting data(call SetData function of the customized QAbstractTableModel) or simpler saying clicking on a cell in the TableView and alter the data in it I take the:
int justRemovedRowIndex = cMagazynView->GetVectMagazyn().at(index)->GetTabView().currentIndex().row();
and everything is working perfectly if sorting is not involved. -
@Slawomir_Piernikowski said in QSortFilterProxyModel, QAbstractItemModel:
int justRemovedRowIndex = cMagazynView->GetVectMagazyn().at(index)->GetTabView().currentIndex().row();
No idea what this means, you gave us 0 context to any of those symbols
-
@VRonin
int justRemovedRowIndex = cMagazynView->GetVectMagazyn().at(index)->GetTabView().currentIndex().row();- cMagazynView - pointer to class which have a member...
... QVector<MagazynView *>vectMagazyn; - vector of MagazynView pointers
GetTabView() -
QTableView &MagazynView::GetTabView() { return *tabView; }
As it is simply to see I want to get row number of the currentIndex row of the TableView
- cMagazynView - pointer to class which have a member...
-
@Slawomir_Piernikowski
As @VRonin has said, you have two approaches to sorting data from the model. Either you do not use aQSortFilterProxyModel
and do the actual sorting yourself on the rows, or you do use it and the sorting happens in the proxy not the underlying data. Then, yes, you must "dereference" to get the actual index in the underlying data from the proxy, and for that you have https://doc.qt.io/qt-5/qsortfilterproxymodel.html#mapToSource. That's just how it is. -
QSortFilterProxyModel
has a method to map back the index of the view to the original model: https://doc.qt.io/qt-5/qsortfilterproxymodel.html#mapToSourceInstead of using
currentIndex().row()
you can useproxyModel->mapToSource(currentIndex()).row()
-
@JonB ok thanks
-
@VRonin ok I will try to use this
-
After a fiew tests it looks like mapToSource did the trick.
auto magazynToBeRemoved = model->GetcMagazynVect().at(cFilterProxyModel->GetVectMagazynFilterProxyModel().at(index)->mapToSource(cMagazynView->GetVectMagazyn().at(index)->GetTabView().currentIndex()).row());
-
@Slawomir_Piernikowski
Good. But do you really write that as a single line in your source code? -
@JonB I know that it can be not so readable for everybody but since I have been working on my soft from allmost 8 monthes I am used to such long lines of code :-)). Usually I devided such a long lines of code like this:
auto magazynToBeRemoved = model->GetcMagazynVect().at(cFilterProxyModel->GetVectMagazynFilterProxyModel().at(index)->
mapToSource(cMagazynView->GetVectMagazyn().at(index)->GetTabView().currentIndex()).row());