diff --git a/qqmlsortfilterproxymodel.cpp b/qqmlsortfilterproxymodel.cpp index d13efdf..c0912f1 100644 --- a/qqmlsortfilterproxymodel.cpp +++ b/qqmlsortfilterproxymodel.cpp @@ -52,6 +52,30 @@ int QQmlSortFilterProxyModel::count() const return rowCount(); } +/*! + \qmlproperty int SortFilterProxyModel::limit + + Limit the total number of results +*/ +int QQmlSortFilterProxyModel::limit() const +{ + return m_limit; +} + +void QQmlSortFilterProxyModel::setLimit(int newLimit) +{ + if (m_limit == newLimit) + return; + + const auto prevCount = count(); + m_limit = newLimit; + emit limitChanged(); + + if (prevCount != count()) { + emit countChanged(); + } +} + /*! \qmlproperty bool SortFilterProxyModel::delayed @@ -234,6 +258,14 @@ QVariant QQmlSortFilterProxyModel::sourceData(const QModelIndex &sourceIndex, in return sourceModel()->data(sourceIndex, role); } +int QQmlSortFilterProxyModel::rowCount(const QModelIndex& parent) const +{ + const auto origLimit = QSortFilterProxyModel::rowCount(parent); + if (m_limit == -1) + return origLimit; + return qMin(m_limit, origLimit); +} + QVariant QQmlSortFilterProxyModel::data(const QModelIndex &index, int role) const { return sourceData(mapToSource(index), role); diff --git a/qqmlsortfilterproxymodel.h b/qqmlsortfilterproxymodel.h index dbe0229..d45274e 100644 --- a/qqmlsortfilterproxymodel.h +++ b/qqmlsortfilterproxymodel.h @@ -23,6 +23,7 @@ class QQmlSortFilterProxyModel : public QSortFilterProxyModel, Q_PROPERTY(int count READ count NOTIFY countChanged) Q_PROPERTY(bool delayed READ delayed WRITE setDelayed NOTIFY delayedChanged) + Q_PROPERTY(int limit READ limit WRITE setLimit NOTIFY limitChanged FINAL) Q_PROPERTY(QString filterRoleName READ filterRoleName WRITE setFilterRoleName NOTIFY filterRoleNameChanged) Q_PROPERTY(QString filterPattern READ filterPattern WRITE setFilterPattern NOTIFY filterPatternChanged) @@ -93,6 +94,7 @@ public: void setSourceModel(QAbstractItemModel *sourceModel) override; Q_SIGNALS: + void limitChanged(); void countChanged(); void delayedChanged(); @@ -105,6 +107,7 @@ Q_SIGNALS: void ascendingSortOrderChanged(); protected: + int rowCount(const QModelIndex &parent = QModelIndex()) const override; bool filterAcceptsRow(int source_row, const QModelIndex& source_parent) const override; bool lessThan(const QModelIndex& source_left, const QModelIndex& source_right) const override; @@ -126,6 +129,9 @@ private Q_SLOTS: void invalidateProxyRoles(); private: + int limit() const; + void setLimit(int newLimit); + QVariantMap modelDataMap(const QModelIndex& modelIndex) const; void onFilterAppended(Filter* filter) override; @@ -153,6 +159,7 @@ private: bool m_invalidateFilterQueued = false; bool m_invalidateQueued = false; bool m_invalidateProxyRolesQueued = false; + int m_limit{-1}; }; }