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. How to filter data in Data Model
QtWS25 Last Chance

How to filter data in Data Model

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
5 Posts 4 Posters 1.6k 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.
  • K Offline
    K Offline
    Kofr
    wrote on last edited by
    #1

    Hi,
    I have a Data Model which contains all items to be shown by View.
    However i want to have 2 views where one shows only items with for example color "black" and other view shows only items with color "red" (these colors is data of items in data model)

    How to make it? Do I have to create Proxy models in C++ or make somehow filtering in QML side?

    E 1 Reply Last reply
    0
    • K Kofr

      Hi,
      I have a Data Model which contains all items to be shown by View.
      However i want to have 2 views where one shows only items with for example color "black" and other view shows only items with color "red" (these colors is data of items in data model)

      How to make it? Do I have to create Proxy models in C++ or make somehow filtering in QML side?

      E Offline
      E Offline
      Eeli K
      wrote on last edited by
      #2

      @Kofr I tried something like this with QStandardItemModel and Quick Controls 2 ComboBox delegate. Not two views, but the idea of hiding in QML is the same. You can hide model items in the delegate based on their state by setting the size to zero and visibility to false. However, the delegate objects still exist so the scroll indicator behaved strangely and keyboard navigation didn't skip the hidden delegate objects but seemed to be lost between the visible objects. As a quick-and-dirty solution it may be acceptable to you depending on the situation. Otherwise a proxy may be the only reliable solution.

      1 Reply Last reply
      0
      • S Offline
        S Offline
        Script22
        wrote on last edited by
        #3

        I have done something similar usinf QSortFilterProxyModel
        basically, you subclass the QSortFilterProxyModel and override the method filterAcceptsRow to return true only when the condition is met.
        And you set the source model for the QSortFilterProxyModel to be your data model then set the view's model to be the proxy model.

        1 Reply Last reply
        0
        • M Offline
          M Offline
          Mammamia
          wrote on last edited by
          #4

          I have created an example which shows how to use QSortFilterProxyModel along with QAbstractListModel.

          Pleasse find the blog post
          and Source code.

          Hope this helps. Happy Coding!!

          K 1 Reply Last reply
          0
          • M Mammamia

            I have created an example which shows how to use QSortFilterProxyModel along with QAbstractListModel.

            Pleasse find the blog post
            and Source code.

            Hope this helps. Happy Coding!!

            K Offline
            K Offline
            Kofr
            wrote on last edited by
            #5

            @Mammamia thx
            I have very large QAbstratItemModel and having two FilterModels is expensive I suppose as QSortFilterProxyModel rescan source model each time it changes data.
            I am still thinking about that.

            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