diff options
Diffstat (limited to 'examples')
-rw-r--r-- | examples/quickcontrols/controls/tableview/src/sortfilterproxymodel.cpp | 33 | ||||
-rw-r--r-- | examples/quickcontrols/controls/tableview/src/sortfilterproxymodel.h | 3 |
2 files changed, 30 insertions, 6 deletions
diff --git a/examples/quickcontrols/controls/tableview/src/sortfilterproxymodel.cpp b/examples/quickcontrols/controls/tableview/src/sortfilterproxymodel.cpp index 1e47f23e..69987a4a 100644 --- a/examples/quickcontrols/controls/tableview/src/sortfilterproxymodel.cpp +++ b/examples/quickcontrols/controls/tableview/src/sortfilterproxymodel.cpp @@ -108,22 +108,43 @@ void SortFilterProxyModel::setFilterRole(const QByteArray &role) QString SortFilterProxyModel::filterString() const { - return filterRegExp().pattern(); + return m_filterString; } void SortFilterProxyModel::setFilterString(const QString &filter) { - setFilterRegExp(QRegExp(filter, filterCaseSensitivity(), static_cast<QRegExp::PatternSyntax>(filterSyntax()))); + m_filterString = filter; + updateRegularExpression(); } SortFilterProxyModel::FilterSyntax SortFilterProxyModel::filterSyntax() const { - return static_cast<FilterSyntax>(filterRegExp().patternSyntax()); + return m_filterSyntax; } void SortFilterProxyModel::setFilterSyntax(SortFilterProxyModel::FilterSyntax syntax) { - setFilterRegExp(QRegExp(filterString(), filterCaseSensitivity(), static_cast<QRegExp::PatternSyntax>(syntax))); + m_filterSyntax = syntax; + updateRegularExpression(); +} + +void SortFilterProxyModel::updateRegularExpression() +{ + auto options = filterCaseSensitivity() == Qt::CaseInsensitive + ? QRegularExpression::CaseInsensitiveOption + : QRegularExpression::NoPatternOption; + + switch (filterSyntax()) { + case RegExp: + setFilterRegularExpression(QRegularExpression(filterString(), options)); + break; + case Wildcard: + setFilterRegularExpression(QRegularExpression(QRegularExpression::wildcardToRegularExpression(filterString()), options)); + break; + case FixedString: + setFilterRegularExpression(QRegularExpression(QRegularExpression::escape(filterString()), options)); + break; + } } QJSValue SortFilterProxyModel::get(int idx) const @@ -165,8 +186,8 @@ QHash<int, QByteArray> SortFilterProxyModel::roleNames() const bool SortFilterProxyModel::filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const { - QRegExp rx = filterRegExp(); - if (rx.isEmpty()) + QRegularExpression rx = filterRegularExpression(); + if (!rx.isValid()) return true; QAbstractItemModel *model = sourceModel(); if (filterRole().isEmpty()) { diff --git a/examples/quickcontrols/controls/tableview/src/sortfilterproxymodel.h b/examples/quickcontrols/controls/tableview/src/sortfilterproxymodel.h index dbc73e84..68cf0690 100644 --- a/examples/quickcontrols/controls/tableview/src/sortfilterproxymodel.h +++ b/examples/quickcontrols/controls/tableview/src/sortfilterproxymodel.h @@ -111,9 +111,12 @@ protected: int roleKey(const QByteArray &role) const; QHash<int, QByteArray> roleNames() const; bool filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const; + void updateRegularExpression(); private: bool m_complete; + FilterSyntax m_filterSyntax; + QString m_filterString; QByteArray m_sortRole; QByteArray m_filterRole; }; |