Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QSortFilterProxyModel not working with a QAbstractListModel derived model.
QtWS25 Last Chance

QSortFilterProxyModel not working with a QAbstractListModel derived model.

Scheduled Pinned Locked Moved General and Desktop
12 Posts 4 Posters 8.1k Views
  • 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.
  • U Offline
    U Offline
    utcenter
    wrote on last edited by
    #1

    @ NewModel model;
    QAbstractItemModel * pm = qobject_cast<QAbstractItemModel *>(&model);
    QSortFilterProxyModel proxy;
    proxy.setSourceModel(pm);
    proxy.setSortRole(NewModel::WordRole);
    proxy.setDynamicSortFilter(true);@

    This is what I tried, but it doesn't seem to work. The ListView in QML is able to pick up the model through the proxy, but there is no sorting. Am I doing something wrong?

    1 Reply Last reply
    0
    • R Offline
      R Offline
      rittk
      wrote on last edited by
      #2

      proxy has allocated on stack. dumb mistake.

      1 Reply Last reply
      0
      • U Offline
        U Offline
        utcenter
        wrote on last edited by
        #3

        [quote author="Konstantin Ritt" date="1367214584"]proxy has allocated on stack. dumb mistake.[/quote]

        Care to elaborate what is so dumb about it and what does it have to do with the sorting proxy not working? Or was that just a random "dumb" statement?

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          You allocate your proxy on the stack, so it will be destroyed at the end of the function hence no sorting. You have to allocate it on the heap to keep it and get your sorting.

          I think Konstantin was just saying the it was a simple mistake that we all do/did from time to time.

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0
          • U Offline
            U Offline
            utcenter
            wrote on last edited by
            #5

            The stack is also the one which instantiates the application so everything allocated there lives for the duration of the application. If Konstantin wasn't so hasty to plant his insult he'd figure it out. And what is worse, absent any information related to the subject, the essence of his post boils down to the contained insult. Oh, let's not forget the abysmal grammar too...

            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

              I have to disagree i.e.

              @
              int main(int argc, char *argv[]
              {
              QApplication app(argc, argv);
              QWidget w;
              {
              NewModel model;
              QAbstractItemModel * pm = qobject_cast<QAbstractItemModel *>(&model);
              QSortFilterProxyModel proxy;
              proxy.setSourceModel(pm);
              proxy.setSortRole(NewModel::WordRole);
              proxy.setDynamicSortFilter(true);
              }
              // both model and proxy have now been destroyed because they are out of scope
              w.show()
              return app.exec();
              }@

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              0
              • U Offline
                U Offline
                utcenter
                wrote on last edited by
                #7

                About 2 seconds of thought should be enough to determine that failure to sort could not possibly be the result of trying to use a deleted local object. I'd expect to get a serious runtime error if I try to use a non-existent object as a model, don't you think?

                Your solidarity is heartwarming but in this particular case misplaced. There is absolutely nothing to hint that the sorting proxy fails to sort because of a dangling reference, nor any possible way to even reference an entirely local object outside of its scope short of an external handle to reach it. I honestly see no point in you defending a hasty "dumb" post absent thought or helpfulness.

                1 Reply Last reply
                0
                • R Offline
                  R Offline
                  rittk
                  wrote on last edited by
                  #8

                  About 2 seconds of thought is enough to imagine few more "dumb" mistakes in the code you didn't show: missing Q_OBJECT macro, not deriving from QAIM, not calling sort() on a column of interest, not supporting NewModel::WordRole role in the model, etc.

                  Check if sorting doesn't work to you with some proven model, i.e. QStandardItemModel: if it doesn't, see what's wrong with the view.

                  1 Reply Last reply
                  0
                  • U Offline
                    U Offline
                    utcenter
                    wrote on last edited by
                    #9

                    bq. About 2 seconds of thought is enough to imagine few more “dumb” mistakes in the code you didn’t show: missing Q_OBJECT macro, not deriving from QAIM, not calling sort() on a column of interest, not supporting NewModel::WordRole role in the model, etc.

                    Konstantin - I see you are firm on the course of making premature statements absent thought:

                    • Q_OBJECT is not missing
                    • I need a list model, thus I implement a QAbstractListModel
                    • there is but a single column in the model
                    • WordRole is supported in the model

                    There is no need to participate if you have nothing of value to contribute with, I am sure the internet offers plenty of other places for you to troll around...

                    1 Reply Last reply
                    0
                    • R Offline
                      R Offline
                      rittk
                      wrote on last edited by
                      #10

                      Sorry, I didn't realize this is a "here are 6 meaningless lines of code, guess what's wrong besides them" game.

                      Check if sorting doesn’t work to you with some proven model, i.e. QStandardItemModel: if it doesn’t, see what’s wrong with the view.

                      Still valuable ^

                      1 Reply Last reply
                      0
                      • U Offline
                        U Offline
                        utcenter
                        wrote on last edited by
                        #11

                        bq. Sorry, I didn’t realize this is a “here are 6 meaningless lines of code, guess what’s wrong besides them” game.

                        Obviously, you were playing a different game right from the start, the game of premature offensive random statements. It is funny and ironic that you above all speak of meaningless lines... Sorry to cut your poopscapade short, but I doubt further input will be much more beneficial than what you "contributed" so far. Thanks a lot, couldn't have done it without you ;)

                        1 Reply Last reply
                        0
                        • S Offline
                          S Offline
                          steno
                          wrote on last edited by
                          #12

                          I had the same problem, but it was because I wasn't calling sort on the proxy. I have come across a problem which I don't understand and one of you might.

                          I have a qml file that worked fine when using a QAbstractListModel, but when I apply a QSortFilterProxyModel to it the qml spits out ReferenceError, documentViewer is not defined.

                          @
                          Rectangle {
                          id: documentViewer
                          color: "#E5E5E5"
                          clip: true
                          visible: false
                          focus:true
                          border.width: 0

                          property bool isInSelectionMode: false
                          
                          property int thumbNailWidth: 152
                          property int thumbNailHeight: 152
                          
                          property int thumbNailTopMargin: 5
                          property int thumbNailBottomMargin: 45
                          property int thumbNailLeftMargin: 22
                          property int thumbNailRightMargin:  22
                          
                          Component {
                              id: modelDelegate
                          
                              Rectangle {
                                  id: imageRect
                                  width: thumbNailLeftMargin + thumbNailRightMargin + thumbNailWidth
                                  height: thumbNailTopMargin + thumbNailBottomMargin + thumbNailHeight
                                  color: "transparent"
                                  border.color: "transparent"
                          
                                  Item {
                                      id: thumbNail
                                      x: thumbNailLeftMargin
                                      y: thumbNailTopMargin
                                      width: documentViewer.thumbNailWidth
                                      height: documentViewer.thumbNailHeight
                          
                                      Image {
                                          id: thumbNailImage
                                          anchors.fill: parent
                                          clip: true
                                          source: imageSource
                                          sourceSize.width:  documentViewer.thumbNailWidth
                                          sourceSize.height: documentViewer.thumbNailHeight
                                      }
                          
                                      MouseArea {
                                          id: mouseArea
                                          anchors.fill: parent
                          
                                          onPressAndHold: {
                                              if(isItemSelectable && !documentViewer.isInSelectionMode)
                                              {
                                                  isItemSelected = !isItemSelected;
                                                  documentViewer.isInSelectionMode = !documentViewer.isInSelectionMode;
                                              }
                                          }
                          
                                          onPressed: {
                                              if(documentViewer.isInSelectionMode && isItemSelectable)
                                              {
                                                  isItemSelected = !isItemSelected;
                                                  mouse.accepted = true;
                                              }
                                          }
                          
                                          onClicked: {
                                              if(documentIndex == 0 && !documentViewer.isInSelectionMode)
                                              {
                                                  DocumentManager.NewDocument();
                                                  documentViewer.done(1);
                                              }
                                              else if(documentIndex != 0 && !documentViewer.isInSelectionMode)
                                              {
                                                  DocumentManager.OpenDocument(name);
                                                  documentViewer.done(1);
                                              }
                                          }
                                      }
                                  }
                             }
                          

                          }

                          @

                          I have a list view that used this delegate, but I haven't showed it in this code snippet. I don't get why when I apply the QALM on the ListView it has no problem determining what documentViewer is, but when I apply QSFPM it gives me a reference error. Any ideas?

                          1 Reply Last reply
                          0

                          • Login

                          • Login or register to search.
                          • First post
                            Last post
                          0
                          • Categories
                          • Recent
                          • Tags
                          • Popular
                          • Users
                          • Groups
                          • Search
                          • Get Qt Extensions
                          • Unsolved