Filter QML Model

  • 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

  • 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.

  • Did you already take a look at QSortFilterProxyModel?

  • 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

    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();

    ClipList m_clips;

    CoreEngine *core;



    @#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->>getFklz());
        return QVariant::fromValue(this->;


    void ClipFilterModel::addClip(ClipElement *clip)
    beginInsertRows(QModelIndex(), rowCount(), rowCount());

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

    void ClipFilterModel::clear()

    bool ClipFilterModel::loadClipList()
    return true;


Log in to reply