diff options
author | Lars Knoll <lars.knoll@qt.io> | 2020-04-02 12:57:56 +0200 |
---|---|---|
committer | Lars Knoll <lars.knoll@qt.io> | 2020-04-15 14:38:23 +0200 |
commit | 48794f5057f49373a7b8803db8bab7131e04575c (patch) | |
tree | a65a490bf0f55ec9b67f8ad74ba92dfa25a7eae2 | |
parent | 52d91508fd391c6dd1eb0312902cd50759eedad2 (diff) |
Port example over to QRegularExpression
Change-Id: I1e15bfa0a2973aabcad78f3eba4bb4903f8f3f6f
Reviewed-by: Alex Blasche <alexander.blasche@qt.io>
5 files changed, 44 insertions, 26 deletions
diff --git a/examples/widgets/doc/src/customsortfiltermodel.qdoc b/examples/widgets/doc/src/customsortfiltermodel.qdoc index 97725ead04..ebbd29a921 100644 --- a/examples/widgets/doc/src/customsortfiltermodel.qdoc +++ b/examples/widgets/doc/src/customsortfiltermodel.qdoc @@ -250,15 +250,15 @@ The \c textFilterChanged() function is called whenever the user changes the filter pattern or the case sensitivity. - We first retrieve the preferred syntax (the QRegExp::PatternSyntax + We first retrieve the preferred syntax (the FilterWidget::PatternSyntax enum is used to interpret the meaning of the given pattern), then we determine the preferred case sensitivity. Based on these preferences and the current filter pattern, we set the proxy - model's \l {QSortFilterProxyModel::}{filterRegExp} property. The - \l {QSortFilterProxyModel::}{filterRegExp} property holds the + model's \l {QSortFilterProxyModel::}{filterRegularExpression} property. The + \l {QSortFilterProxyModel::}{filterRegularExpression} property holds the regular expression used to filter the contents of the source model. Note that calling QSortFilterProxyModel's \l - {QSortFilterProxyModel::}{setFilterRegExp()} function also updates + {QSortFilterProxyModel::}{setFilterRegularExpression()} function also updates the model. \snippet itemviews/customsortfiltermodel/window.cpp 9 diff --git a/examples/widgets/itemviews/customsortfiltermodel/filterwidget.cpp b/examples/widgets/itemviews/customsortfiltermodel/filterwidget.cpp index 302042a6b3..bfcc8f84fb 100644 --- a/examples/widgets/itemviews/customsortfiltermodel/filterwidget.cpp +++ b/examples/widgets/itemviews/customsortfiltermodel/filterwidget.cpp @@ -72,18 +72,18 @@ FilterWidget::FilterWidget(QWidget *parent) menu->addSeparator(); m_patternGroup->setExclusive(true); - QAction *patternAction = menu->addAction("Fixed String"); - patternAction->setData(QVariant(int(QRegExp::FixedString))); + QAction *patternAction = menu->addAction("Regular Expression"); patternAction->setCheckable(true); patternAction->setChecked(true); + patternAction->setData(QVariant(int(RegularExpression))); m_patternGroup->addAction(patternAction); - patternAction = menu->addAction("Regular Expression"); + patternAction = menu->addAction("Wildcard"); patternAction->setCheckable(true); - patternAction->setData(QVariant(int(QRegExp::RegExp2))); + patternAction->setData(QVariant(int(Wildcard))); m_patternGroup->addAction(patternAction); - patternAction = menu->addAction("Wildcard"); + patternAction = menu->addAction("Fixed String"); + patternAction->setData(QVariant(int(FixedString))); patternAction->setCheckable(true); - patternAction->setData(QVariant(int(QRegExp::Wildcard))); m_patternGroup->addAction(patternAction); connect(m_patternGroup, &QActionGroup::triggered, this, &FilterWidget::filterChanged); @@ -113,17 +113,17 @@ void FilterWidget::setCaseSensitivity(Qt::CaseSensitivity cs) m_caseSensitivityAction->setChecked(cs == Qt::CaseSensitive); } -static inline QRegExp::PatternSyntax patternSyntaxFromAction(const QAction *a) +static inline FilterWidget::PatternSyntax patternSyntaxFromAction(const QAction *a) { - return static_cast<QRegExp::PatternSyntax>(a->data().toInt()); + return static_cast<FilterWidget::PatternSyntax>(a->data().toInt()); } -QRegExp::PatternSyntax FilterWidget::patternSyntax() const +FilterWidget::PatternSyntax FilterWidget::patternSyntax() const { return patternSyntaxFromAction(m_patternGroup->checkedAction()); } -void FilterWidget::setPatternSyntax(QRegExp::PatternSyntax s) +void FilterWidget::setPatternSyntax(PatternSyntax s) { const QList<QAction*> actions = m_patternGroup->actions(); for (QAction *a : actions) { diff --git a/examples/widgets/itemviews/customsortfiltermodel/filterwidget.h b/examples/widgets/itemviews/customsortfiltermodel/filterwidget.h index 70214b862e..0404d670b2 100644 --- a/examples/widgets/itemviews/customsortfiltermodel/filterwidget.h +++ b/examples/widgets/itemviews/customsortfiltermodel/filterwidget.h @@ -52,28 +52,32 @@ #define FILTERWIDGET_H #include <QLineEdit> -#include <QRegExp> QT_BEGIN_NAMESPACE class QAction; class QActionGroup; QT_END_NAMESPACE -Q_DECLARE_METATYPE(QRegExp::PatternSyntax) - class FilterWidget : public QLineEdit { Q_OBJECT Q_PROPERTY(Qt::CaseSensitivity caseSensitivity READ caseSensitivity WRITE setCaseSensitivity) - Q_PROPERTY(QRegExp::PatternSyntax patternSyntax READ patternSyntax WRITE setPatternSyntax) + Q_PROPERTY(PatternSyntax patternSyntax READ patternSyntax WRITE setPatternSyntax) public: explicit FilterWidget(QWidget *parent = nullptr); Qt::CaseSensitivity caseSensitivity() const; void setCaseSensitivity(Qt::CaseSensitivity); - QRegExp::PatternSyntax patternSyntax() const; - void setPatternSyntax(QRegExp::PatternSyntax); + enum PatternSyntax { + RegularExpression, + Wildcard, + FixedString + }; + Q_ENUM(PatternSyntax) + + PatternSyntax patternSyntax() const; + void setPatternSyntax(PatternSyntax); signals: void filterChanged(); diff --git a/examples/widgets/itemviews/customsortfiltermodel/mysortfilterproxymodel.cpp b/examples/widgets/itemviews/customsortfiltermodel/mysortfilterproxymodel.cpp index fd17876f2b..da31f8e361 100644 --- a/examples/widgets/itemviews/customsortfiltermodel/mysortfilterproxymodel.cpp +++ b/examples/widgets/itemviews/customsortfiltermodel/mysortfilterproxymodel.cpp @@ -83,8 +83,8 @@ bool MySortFilterProxyModel::filterAcceptsRow(int sourceRow, QModelIndex index1 = sourceModel()->index(sourceRow, 1, sourceParent); QModelIndex index2 = sourceModel()->index(sourceRow, 2, sourceParent); - return (sourceModel()->data(index0).toString().contains(filterRegExp()) - || sourceModel()->data(index1).toString().contains(filterRegExp())) + return (sourceModel()->data(index0).toString().contains(filterRegularExpression()) + || sourceModel()->data(index1).toString().contains(filterRegularExpression())) && dateInRange(sourceModel()->data(index2).toDate()); } //! [3] diff --git a/examples/widgets/itemviews/customsortfiltermodel/window.cpp b/examples/widgets/itemviews/customsortfiltermodel/window.cpp index 3356c971ad..3423ac4ee9 100644 --- a/examples/widgets/itemviews/customsortfiltermodel/window.cpp +++ b/examples/widgets/itemviews/customsortfiltermodel/window.cpp @@ -148,10 +148,24 @@ void Window::setSourceModel(QAbstractItemModel *model) //! [8] void Window::textFilterChanged() { - QRegExp regExp(filterWidget->text(), - filterWidget->caseSensitivity(), - filterWidget->patternSyntax()); - proxyModel->setFilterRegExp(regExp); + FilterWidget::PatternSyntax s = filterWidget->patternSyntax(); + QString pattern = filterWidget->text(); + switch (s) { + case FilterWidget::Wildcard: + pattern = QRegularExpression::wildcardToRegularExpression(pattern); + break; + case FilterWidget::FixedString: + pattern = QRegularExpression::escape(pattern); + break; + default: + break; + } + + QRegularExpression::PatternOptions options = QRegularExpression::NoPatternOption; + if (filterWidget->caseSensitivity() == Qt::CaseInsensitive) + options |= QRegularExpression::CaseInsensitiveOption; + QRegularExpression regularExpression(pattern, options); + proxyModel->setFilterRegularExpression(regularExpression); } //! [8] |