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

QSortFilterProxyModel, QAbstractItemModel



  • Hi
    When data sort itself in TableView for example when clicking on a header of a column arrow or editing and setData in a cell of the TableView the order of model objects in the model container not correspond the order of the data in a TableView.
    I have customized QSortFilterProxyModel so for sotring is responsible QSortFilterProxyModel::Sort function.

    I give some example to let somebody to easier to understand what is about:
    Hereunder is printed from model container just after sort was executed on the TableView:
    (data order in the model container)
    Magazyn categories: "ZZZ"
    Magazyn categories: "XXX"
    Magazyn categories: "QQQ"
    Magazyn categories: "AAA"

    (data order in the tableView is what I see in the TableView)
    Magazyn categories: "AAA"
    Magazyn categories: "QQQ"
    Magazyn categories: "XXX"
    Magazyn categories: "ZZZ"

    So the order of the data in the Tableview is different than in the model(QAbstractTableModel). Why is it so?



  • @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 QAbstractTableModel

    For 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 reimplement sort in your custom QAbstractItemModel emitting layoutAboutToBeChanged and layoutChanged and calling changePersistentIndexes



  • @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();

    1. 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



  • @Slawomir_Piernikowski
    As @VRonin has said, you have two approaches to sorting data from the model. Either you do not use a QSortFilterProxyModel 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#mapToSource

    Instead of using currentIndex().row() you can use proxyModel->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());
    

Log in to reply