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?

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



  • 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;
    }

    @


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.