From 52d91508fd391c6dd1eb0312902cd50759eedad2 Mon Sep 17 00:00:00 2001 From: Lars Knoll Date: Mon, 30 Mar 2020 12:38:59 +0200 Subject: Convert the example to use QRegularExpression Change-Id: I7a4259ac43e59a8f50ee1c5072a3da5b977d8bfe Reviewed-by: Alex Blasche Reviewed-by: Samuel Gaist --- .../itemviews/basicsortfiltermodel/window.cpp | 41 +++++++++++++--------- .../itemviews/basicsortfiltermodel/window.h | 8 ++++- 2 files changed, 32 insertions(+), 17 deletions(-) (limited to 'examples/widgets') diff --git a/examples/widgets/itemviews/basicsortfiltermodel/window.cpp b/examples/widgets/itemviews/basicsortfiltermodel/window.cpp index bfc9044724..5957ea7a1a 100644 --- a/examples/widgets/itemviews/basicsortfiltermodel/window.cpp +++ b/examples/widgets/itemviews/basicsortfiltermodel/window.cpp @@ -74,9 +74,9 @@ Window::Window() filterPatternLabel->setBuddy(filterPatternLineEdit); filterSyntaxComboBox = new QComboBox; - filterSyntaxComboBox->addItem(tr("Regular expression"), QRegExp::RegExp); - filterSyntaxComboBox->addItem(tr("Wildcard"), QRegExp::Wildcard); - filterSyntaxComboBox->addItem(tr("Fixed string"), QRegExp::FixedString); + filterSyntaxComboBox->addItem(tr("Regular expression"), RegularExpression); + filterSyntaxComboBox->addItem(tr("Wildcard"), Wildcard); + filterSyntaxComboBox->addItem(tr("Fixed string"), FixedString); filterSyntaxLabel = new QLabel(tr("Filter &syntax:")); filterSyntaxLabel->setBuddy(filterSyntaxComboBox); @@ -88,13 +88,13 @@ Window::Window() filterColumnLabel->setBuddy(filterColumnComboBox); connect(filterPatternLineEdit, &QLineEdit::textChanged, - this, &Window::filterRegExpChanged); + this, &Window::filterRegularExpressionChanged); connect(filterSyntaxComboBox, &QComboBox::currentIndexChanged, - this, &Window::filterRegExpChanged); + this, &Window::filterRegularExpressionChanged); connect(filterColumnComboBox, &QComboBox::currentIndexChanged, this, &Window::filterColumnChanged); connect(filterCaseSensitivityCheckBox, &QAbstractButton::toggled, - this, &Window::filterRegExpChanged); + this, &Window::filterRegularExpressionChanged); connect(sortCaseSensitivityCheckBox, &QAbstractButton::toggled, this, &Window::sortChanged); @@ -141,17 +141,26 @@ void Window::setSourceModel(QAbstractItemModel *model) sourceView->setModel(model); } -void Window::filterRegExpChanged() +void Window::filterRegularExpressionChanged() { - QRegExp::PatternSyntax syntax = - QRegExp::PatternSyntax(filterSyntaxComboBox->itemData( - filterSyntaxComboBox->currentIndex()).toInt()); - Qt::CaseSensitivity caseSensitivity = - filterCaseSensitivityCheckBox->isChecked() ? Qt::CaseSensitive - : Qt::CaseInsensitive; - - QRegExp regExp(filterPatternLineEdit->text(), caseSensitivity, syntax); - proxyModel->setFilterRegExp(regExp); + Syntax s = Syntax(filterSyntaxComboBox->itemData(filterSyntaxComboBox->currentIndex()).toInt()); + QString pattern = filterPatternLineEdit->text(); + switch (s) { + case Wildcard: + pattern = QRegularExpression::wildcardToRegularExpression(pattern); + break; + case FixedString: + pattern = QRegularExpression::escape(pattern); + break; + default: + break; + } + + QRegularExpression::PatternOptions options = QRegularExpression::NoPatternOption; + if (!filterCaseSensitivityCheckBox->isChecked()) + options |= QRegularExpression::CaseInsensitiveOption; + QRegularExpression regularExpression(pattern, options); + proxyModel->setFilterRegularExpression(regularExpression); } void Window::filterColumnChanged() diff --git a/examples/widgets/itemviews/basicsortfiltermodel/window.h b/examples/widgets/itemviews/basicsortfiltermodel/window.h index 861c791dd9..137b0de712 100644 --- a/examples/widgets/itemviews/basicsortfiltermodel/window.h +++ b/examples/widgets/itemviews/basicsortfiltermodel/window.h @@ -74,7 +74,7 @@ public: void setSourceModel(QAbstractItemModel *model); private slots: - void filterRegExpChanged(); + void filterRegularExpressionChanged(); void filterColumnChanged(); void sortChanged(); @@ -91,6 +91,12 @@ private: QLabel *filterSyntaxLabel; QLabel *filterColumnLabel; QLineEdit *filterPatternLineEdit; + enum Syntax { + RegularExpression, + Wildcard, + FixedString + }; + QComboBox *filterSyntaxComboBox; QComboBox *filterColumnComboBox; }; -- cgit v1.2.3