Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. QT QML How to chain QAbstractListModel and QSortFilterProxyModel
Qt 6.11 is out! See what's new in the release blog

QT QML How to chain QAbstractListModel and QSortFilterProxyModel

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
4 Posts 3 Posters 168 Views 2 Watching
  • 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.
  • Z Offline
    Z Offline
    Zapfyr
    wrote last edited by
    #1

    Hi,

    I'm working on a project where I get a large JSON as input, this JSON can change at any time. Items can be removed, added and changed.

    I want to to be able to access this data in QML. And I want the QML to be able to sort and filter the data. And I want to do it the right way.

    I have broken down the input data into a view model in C++, which consists of a number of implementations of QAbstractListModels. As I do not want to expose QLists with elements to QML. And I want to be able to chain those with filters and sorters. So I have implemented a number of QSortFilterProxyModels for that purpose. One of these I want to be able to limit the number of outputs.

    So I want something like: List -> Some kind of filtering -> Some kind of sorting -> Limit the output to X items.

    But I run in trouble when the input data changes. The output contains empty items. To make it work I have use beginResetModel and endResetModel, instead functions like beginRemoveRows, endRemoveRows etc. Which seems to defeat the purpose of using a QAbstractListModel.

    I have created a small sample project: https://gitlab.com/FinitelyFailed/qt-list-limited
    swappy-20260326_224029.png

    jeremy_kJ 1 Reply Last reply
    0
    • GrecKoG Offline
      GrecKoG Offline
      GrecKo
      Qt Champions 2018
      wrote last edited by
      #2

      I haven't ran your example but note that using QSFPM is not a good base for your LimitFilter unless you use some workarounds.
      QSFPM doesn't re-run the filters for accepted rows when a new row is added.
      If you are limiting to 1 row, and a new row is inserted first, your model will have 2 rows, the previous first row and the new one.
      You could fake a dataChanged on the previous row to force a call to filterAcceptsRow.

      If you want a 3rd party model doing the work of your JSON to QAIM there's https://github.com/benlau/qsyncable and its QSJsonListModel.
      There's also now a new SortFilterProxyModel available in QML, for previous Qt versions there are multiple on Github.
      For the LimitFilter I don't know of any from the top of my head but I guess there are some available, or reimplement it yourself but keep the QSFPM limitation in minds. It's was designed to work on the intrinsic data of a row, not it's relative position in the model.

      1 Reply Last reply
      0
      • Z Offline
        Z Offline
        Zapfyr
        wrote last edited by
        #3

        Thanks for your answer. We use 6.5.3 and for some projects 6.8.3.
        I'll checkout QSJsonListModel.

        What the difference between the old SortFilterProxyModel and the new one?

        Where should I start to implement a limit filter from scratch?

        1 Reply Last reply
        0
        • Z Zapfyr

          Hi,

          I'm working on a project where I get a large JSON as input, this JSON can change at any time. Items can be removed, added and changed.

          I want to to be able to access this data in QML. And I want the QML to be able to sort and filter the data. And I want to do it the right way.

          I have broken down the input data into a view model in C++, which consists of a number of implementations of QAbstractListModels. As I do not want to expose QLists with elements to QML. And I want to be able to chain those with filters and sorters. So I have implemented a number of QSortFilterProxyModels for that purpose. One of these I want to be able to limit the number of outputs.

          So I want something like: List -> Some kind of filtering -> Some kind of sorting -> Limit the output to X items.

          But I run in trouble when the input data changes. The output contains empty items. To make it work I have use beginResetModel and endResetModel, instead functions like beginRemoveRows, endRemoveRows etc. Which seems to defeat the purpose of using a QAbstractListModel.

          I have created a small sample project: https://gitlab.com/FinitelyFailed/qt-list-limited
          swappy-20260326_224029.png

          jeremy_kJ Offline
          jeremy_kJ Offline
          jeremy_k
          wrote last edited by
          #4

          @Zapfyr said in QT QML How to chain QAbstractListModel and QSortFilterProxyModel:

          Limit the output to X items.

          This sounds like the job of the view. Provide space to display X items, and omit the ability to navigate further.

          Asking a question about code? http://eel.is/iso-c++/testcase/

          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