Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Solved QSortFilterProxyModel, QAbstractItemModel

    General and Desktop
    3
    20
    1397
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • S
      Slawomir_Piernikowski last edited by Slawomir_Piernikowski

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

      VRonin 1 Reply Last reply Reply Quote 0
      • VRonin
        VRonin @Slawomir_Piernikowski last edited by

        @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)

        "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
        ~Napoleon Bonaparte

        On a crusade to banish setIndexWidget() from the holy land of Qt

        S 1 Reply Last reply Reply Quote 2
        • S
          Slawomir_Piernikowski @VRonin last edited by

          @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????

          1 Reply Last reply Reply Quote 0
          • VRonin
            VRonin last edited by

            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

            "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
            ~Napoleon Bonaparte

            On a crusade to banish setIndexWidget() from the holy land of Qt

            S 1 Reply Last reply Reply Quote 3
            • S
              Slawomir_Piernikowski @VRonin last edited by Slawomir_Piernikowski

              @VRonin ok but I need to have model data reorded the same way as the data are sorted and seen in the view

              1 Reply Last reply Reply Quote 0
              • VRonin
                VRonin last edited by VRonin

                Then you have to ditch the QSortFilterProxyModel and reimplement sort in your custom QAbstractItemModel emitting layoutAboutToBeChanged and layoutChanged and calling changePersistentIndexes

                "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                ~Napoleon Bonaparte

                On a crusade to banish setIndexWidget() from the holy land of Qt

                S 1 Reply Last reply Reply Quote 2
                • S
                  Slawomir_Piernikowski @VRonin last edited by Slawomir_Piernikowski

                  @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??????

                  JonB 1 Reply Last reply Reply Quote 0
                  • JonB
                    JonB @Slawomir_Piernikowski last edited by JonB

                    @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?

                    S 1 Reply Last reply Reply Quote 2
                    • S
                      Slawomir_Piernikowski @JonB last edited by Slawomir_Piernikowski

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

                      VRonin 1 Reply Last reply Reply Quote 0
                      • VRonin
                        VRonin @Slawomir_Piernikowski last edited by

                        @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

                        "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                        ~Napoleon Bonaparte

                        On a crusade to banish setIndexWidget() from the holy land of Qt

                        S 1 Reply Last reply Reply Quote 0
                        • S
                          Slawomir_Piernikowski @VRonin last edited by

                          @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

                          JonB 1 Reply Last reply Reply Quote 0
                          • JonB
                            JonB @Slawomir_Piernikowski last edited by JonB

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

                            S 1 Reply Last reply Reply Quote 1
                            • VRonin
                              VRonin last edited by VRonin

                              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()

                              "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                              ~Napoleon Bonaparte

                              On a crusade to banish setIndexWidget() from the holy land of Qt

                              S 1 Reply Last reply Reply Quote 2
                              • S
                                Slawomir_Piernikowski @JonB last edited by

                                @JonB ok thanks

                                1 Reply Last reply Reply Quote 0
                                • S
                                  Slawomir_Piernikowski @VRonin last edited by

                                  @VRonin ok I will try to use this

                                  1 Reply Last reply Reply Quote 0
                                  • S
                                    Slawomir_Piernikowski last edited by

                                    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());
                                    
                                    JonB 1 Reply Last reply Reply Quote 2
                                    • JonB
                                      JonB @Slawomir_Piernikowski last edited by

                                      @Slawomir_Piernikowski
                                      Good. But do you really write that as a single line in your source code?

                                      S 1 Reply Last reply Reply Quote 1
                                      • S
                                        Slawomir_Piernikowski @JonB last edited by Slawomir_Piernikowski

                                        @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());
                                        
                                        1 Reply Last reply Reply Quote 0
                                        • First post
                                          Last post