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. Filter QML Model
Qt 6.11 is out! See what's new in the release blog

Filter QML Model

Scheduled Pinned Locked Moved QML and Qt Quick
4 Posts 2 Posters 5.4k Views 1 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.
  • S Offline
    S Offline
    Schneidi
    wrote on last edited by
    #1

    Hello,

    I use a class inheriting QAbstractListModel as model for a qml ListView.
    What I need is to filter this model.

    Currently I have one Model which should be used by several ListViews but
    each listview is supposed to display only a subset of the model data.

    I was able to filter the model by QAbstractItemModel::match() but how can I use
    a QModelIndexList to pass the data subsets to the qml listviews ?

    Thanks for help guys

    1 Reply Last reply
    0
    • S Offline
      S Offline
      Schneidi
      wrote on last edited by
      #2

      No ideas on this one ?

      Based on a QAbstractListModel how can i create subset models for a listview. I'm not able to use my Model class as return parameter to build temporary models.

      1 Reply Last reply
      0
      • J Offline
        J Offline
        Jens
        wrote on last edited by
        #3

        Did you already take a look at QSortFilterProxyModel?

        https://qt-project.org/doc/qt-5.1/qtcore/qsortfilterproxymodel.html

        1 Reply Last reply
        0
        • S Offline
          S Offline
          Schneidi
          wrote on last edited by
          #4

          Hi jens,

          I've tried to port my QAbstractListModel to a QSortFilterProxyModel.
          But it keeps crashing as soon as i try to set data or accessing the ClipList m_clips member in any way.

          Can the QSortFilterProxyModel provide more than one filtered model based on one set of data ? For example I need two lists based on the same model both with a different filter aktive.

          @
          class CoreEngine;

          class ClipFilterModel : public QSortFilterProxyModel
          {
          Q_OBJECT

          public:
          ClipFilterModel(CoreEngine *parent = 0);

          enum Roles {ObjectRole = Qt::UserRole, Fklz};
          
          QHash<int, QByteArray> roleNames() const {
              QHash<int, QByteArray> roles;
              roles[ObjectRole] = "object";
              roles[Fklz] = "fklz";
              return roles;
          }
          
          QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const;
          
          void addClip(ClipElement *clip);
          
          int rowCount(const QModelIndex & parent = QModelIndex()) const;
          void clear();
          
          inline ClipList *getModelClipList() {return &this->m_clips;}
          
          bool loadClipList();
          

          private:
          ClipList m_clips;

          CoreEngine *core;
          

          };
          @

          .cpp

          @#include "clipfiltermodel.h"

          #include <QtWidgets>

          //! [0]
          ClipFilterModel::ClipFilterModel(CoreEngine *parent)
          : QSortFilterProxyModel(parent)
          {
          this->core = parent;
          }

          QVariant ClipFilterModel::data(const QModelIndex &index, int role) const {
          if (!index.isValid())
          return QVariant();

          if (index.row() >= m_clips.size() || index.row() < 0)
              return QVariant();
          
          if (role == Fklz)
              return QVariant::fromValue(this->m_clips.at(index.row())->getFklz());
          else
              return QVariant::fromValue(this->m_clips.at(index.row()));
          

          }

          void ClipFilterModel::addClip(ClipElement *clip)
          {
          beginInsertRows(QModelIndex(), rowCount(), rowCount());
          m_clips.append(clip);
          endInsertRows();
          }

          int ClipFilterModel::rowCount(const QModelIndex & /* parent */) const
          {
          return m_clips.count();
          }

          void ClipFilterModel::clear()
          {
          beginResetModel();
          this->m_clips.clear();
          endResetModel();
          }

          bool ClipFilterModel::loadClipList()
          {
          beginResetModel();
          this->clear();
          this->m_clips.append(this->core->getDBHandle()->getClipList());
          endResetModel();
          return true;
          }

          @

          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