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. How to sort QAbstractListModel
QtWS25 Last Chance

How to sort QAbstractListModel

Scheduled Pinned Locked Moved General and Desktop
12 Posts 4 Posters 8.2k 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.
  • T Offline
    T Offline
    tokafr
    wrote on 16 Jan 2015, 12:24 last edited by
    #1

    Hello all.

    I have data represented with QAbstractListModel and displayed on QlistView.
    but the list is unsorted, how can I sort it?

    1 Reply Last reply
    0
    • T Offline
      T Offline
      tokafr
      wrote on 16 Jan 2015, 12:34 last edited by
      #2

      I cannot see sort() method for QAbstractListModel

      1 Reply Last reply
      0
      • R Offline
        R Offline
        raven-worx
        Moderators
        wrote on 16 Jan 2015, 12:39 last edited by
        #3

        what do you mean you "cannot see sort() method"?

        Do you mean in the Qt source code?
        Its implemented in QAbstractItemModel class. And it's implemented empty there of course.
        Since it's an Abstract class it's not clear how the data is stored inside the model and thus it would be impossible to be implemented there.

        For example you can use QStringListModel which already has it implemented, since it also already has the data storage implemented.

        But if you need to subclass QAbstractListModel you also have to implement the sort() method yourself.

        --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
        If you have a question please use the forum so others can benefit from the solution in the future

        1 Reply Last reply
        0
        • T Offline
          T Offline
          tokafr
          wrote on 16 Jan 2015, 12:48 last edited by
          #4

          Do I need to write operators for my class?I have a list of object as data

          1 Reply Last reply
          0
          • A Offline
            A Offline
            andre
            wrote on 16 Jan 2015, 12:50 last edited by
            #5

            I'd just use a QSortFilterProxyModel between my source model and my view.

            1 Reply Last reply
            0
            • J Offline
              J Offline
              Jeroentjehome
              wrote on 16 Jan 2015, 12:55 last edited by
              #6

              The abstractItemModel does NOT hold the data. It's just a defined class so that a View is able to call predetermined functions to set/read data etc. You should subclass het AbstractItemModel and write code in your class that holds the data. e.g. a QList.
              The sorting of the View is not relevant to the data in the model. Like Andre, use a SortFilterProxy between your model and the View. The SortFilter knows which item in the View is connected with which item in the Model.
              When data is inserted (externally and not from the view) you should call the reset function of the View so to reread all data. Better to read the Model/View stuff first.

              Greetz, Jeroen

              1 Reply Last reply
              0
              • A Offline
                A Offline
                andre
                wrote on 16 Jan 2015, 12:59 last edited by
                #7

                Better not call reset() if you can help it. And also better not use a QAbstractItemModel (subclass) as a data store. Instead, use it as an adaptor between a data store suitable for your application and the item view system in Qt.

                1 Reply Last reply
                0
                • J Offline
                  J Offline
                  Jeroentjehome
                  wrote on 19 Jan 2015, 08:14 last edited by
                  #8

                  That's even better ;-)

                  Greetz, Jeroen

                  1 Reply Last reply
                  0
                  • T Offline
                    T Offline
                    tokafr
                    wrote on 19 Jan 2015, 12:20 last edited by
                    #9

                    I tried this.
                    @
                    QSortFilterProxyModel *proxy = new QSortFilterProxyModel;
                    proxy->setSourceModel(model);// inherists QAbstractListmodel
                    proxy->sort(0);
                    view->setModel(proxy);//this is QLisView;

                    view->setitemDelegate(delegate);// to paint the view;
                    @
                    but no result. the output is the same(unsorted list)

                    1 Reply Last reply
                    0
                    • A Offline
                      A Offline
                      andre
                      wrote on 19 Jan 2015, 12:22 last edited by
                      #10

                      Strange. That should work. Could you try setting the sortRole to a role that actually contains the data to be sorted?

                      1 Reply Last reply
                      0
                      • T Offline
                        T Offline
                        tokafr
                        wrote on 19 Jan 2015, 12:43 last edited by
                        #11

                        it is a bit unclear for me, How can the sort() do sorting of my data when the data
                        is list of my class, it isn't string list. how can he compare the objects to sort?

                        1 Reply Last reply
                        0
                        • A Offline
                          A Offline
                          andre
                          wrote on 19 Jan 2015, 13:01 last edited by
                          #12

                          The proxy class creates a mapping between the (unsorted) source model, and the sorted representation it presents to its clients. Of course, it can only (by default) sort items it understands: strings, dates, numbers... If you have other items, you will need to subclass QSFPM and reimplement its lessThan virtual function to handle those custom items. Or, you create a role for the sort proxy to use as the sort key, with a data type that the default QSFPM does understand, and then set that role as the sortRole.

                          1 Reply Last reply
                          0

                          7/12

                          16 Jan 2015, 12:59

                          • Login

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