Port example over to QRegularExpression
Change-Id: I1e15bfa0a2973aabcad78f3eba4bb4903f8f3f6f Reviewed-by: Alex Blasche <alexander.blasche@qt.io>
This commit is contained in:
parent
52d91508fd
commit
48794f5057
@ -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
|
||||
|
@ -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);
|
||||
m_patternGroup->addAction(patternAction);
|
||||
patternAction = menu->addAction("Regular Expression");
|
||||
patternAction->setCheckable(true);
|
||||
patternAction->setData(QVariant(int(QRegExp::RegExp2)));
|
||||
patternAction->setData(QVariant(int(RegularExpression)));
|
||||
m_patternGroup->addAction(patternAction);
|
||||
patternAction = menu->addAction("Wildcard");
|
||||
patternAction->setCheckable(true);
|
||||
patternAction->setData(QVariant(int(QRegExp::Wildcard)));
|
||||
patternAction->setData(QVariant(int(Wildcard)));
|
||||
m_patternGroup->addAction(patternAction);
|
||||
patternAction = menu->addAction("Fixed String");
|
||||
patternAction->setData(QVariant(int(FixedString)));
|
||||
patternAction->setCheckable(true);
|
||||
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) {
|
||||
|
@ -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();
|
||||
|
@ -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]
|
||||
|
@ -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]
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user