From a3d95d2290b3a150ff51ced7b4c868fb0df176ea Mon Sep 17 00:00:00 2001 From: Pierre-Yves Siret Date: Sun, 8 Jan 2017 22:49:07 +0100 Subject: [PATCH] Check the roles after a row is added for a model with no roles intially Fixes #13 --- qqmlsortfilterproxymodel.cpp | 13 +++++++++++++ qqmlsortfilterproxymodel.h | 4 ++++ 2 files changed, 17 insertions(+) diff --git a/qqmlsortfilterproxymodel.cpp b/qqmlsortfilterproxymodel.cpp index 0e6e7f6..3adbb70 100644 --- a/qqmlsortfilterproxymodel.cpp +++ b/qqmlsortfilterproxymodel.cpp @@ -194,6 +194,13 @@ bool QQmlSortFilterProxyModel::lessThan(const QModelIndex& source_left, const QM return source_left.row() < source_right.row(); } +void QQmlSortFilterProxyModel::resetInternalData() +{ + QSortFilterProxyModel::resetInternalData(); + if (roleNames().isEmpty()) // workaround for when a model has no roles and roles are added when the model is populated (ListModel) + connect(this, &QAbstractItemModel::rowsAboutToBeInserted, this, &QQmlSortFilterProxyModel::initRoles); +} + void QQmlSortFilterProxyModel::invalidateFilter() { if (m_completed) @@ -231,6 +238,12 @@ void QQmlSortFilterProxyModel::updateRoles() updateSortRole(); } +void QQmlSortFilterProxyModel::initRoles() +{ + disconnect(this, &QAbstractItemModel::rowsAboutToBeInserted, this , &QQmlSortFilterProxyModel::initRoles); + resetInternalData(); +} + QVariantMap QQmlSortFilterProxyModel::modelDataMap(const QModelIndex& modelIndex) const { QVariantMap map; diff --git a/qqmlsortfilterproxymodel.h b/qqmlsortfilterproxymodel.h index a982746..8364767 100644 --- a/qqmlsortfilterproxymodel.h +++ b/qqmlsortfilterproxymodel.h @@ -84,12 +84,16 @@ protected: bool filterAcceptsRow(int source_row, const QModelIndex& source_parent) const override; bool lessThan(const QModelIndex& source_left, const QModelIndex& source_right) const override; +protected Q_SLOTS: + void resetInternalData(); + private Q_SLOTS: void invalidateFilter(); void invalidate(); void updateFilterRole(); void updateSortRole(); void updateRoles(); + void initRoles(); private: QVariantMap modelDataMap(const QModelIndex& modelIndex) const;