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
|
The \c textFilterChanged() function is called whenever the user
|
||||||
changes the filter pattern or the case sensitivity.
|
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
|
enum is used to interpret the meaning of the given pattern), then
|
||||||
we determine the preferred case sensitivity. Based on these
|
we determine the preferred case sensitivity. Based on these
|
||||||
preferences and the current filter pattern, we set the proxy
|
preferences and the current filter pattern, we set the proxy
|
||||||
model's \l {QSortFilterProxyModel::}{filterRegExp} property. The
|
model's \l {QSortFilterProxyModel::}{filterRegularExpression} property. The
|
||||||
\l {QSortFilterProxyModel::}{filterRegExp} property holds the
|
\l {QSortFilterProxyModel::}{filterRegularExpression} property holds the
|
||||||
regular expression used to filter the contents of the source
|
regular expression used to filter the contents of the source
|
||||||
model. Note that calling QSortFilterProxyModel's \l
|
model. Note that calling QSortFilterProxyModel's \l
|
||||||
{QSortFilterProxyModel::}{setFilterRegExp()} function also updates
|
{QSortFilterProxyModel::}{setFilterRegularExpression()} function also updates
|
||||||
the model.
|
the model.
|
||||||
|
|
||||||
\snippet itemviews/customsortfiltermodel/window.cpp 9
|
\snippet itemviews/customsortfiltermodel/window.cpp 9
|
||||||
|
@ -72,18 +72,18 @@ FilterWidget::FilterWidget(QWidget *parent)
|
|||||||
|
|
||||||
menu->addSeparator();
|
menu->addSeparator();
|
||||||
m_patternGroup->setExclusive(true);
|
m_patternGroup->setExclusive(true);
|
||||||
QAction *patternAction = menu->addAction("Fixed String");
|
QAction *patternAction = menu->addAction("Regular Expression");
|
||||||
patternAction->setData(QVariant(int(QRegExp::FixedString)));
|
|
||||||
patternAction->setCheckable(true);
|
patternAction->setCheckable(true);
|
||||||
patternAction->setChecked(true);
|
patternAction->setChecked(true);
|
||||||
m_patternGroup->addAction(patternAction);
|
patternAction->setData(QVariant(int(RegularExpression)));
|
||||||
patternAction = menu->addAction("Regular Expression");
|
|
||||||
patternAction->setCheckable(true);
|
|
||||||
patternAction->setData(QVariant(int(QRegExp::RegExp2)));
|
|
||||||
m_patternGroup->addAction(patternAction);
|
m_patternGroup->addAction(patternAction);
|
||||||
patternAction = menu->addAction("Wildcard");
|
patternAction = menu->addAction("Wildcard");
|
||||||
patternAction->setCheckable(true);
|
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);
|
m_patternGroup->addAction(patternAction);
|
||||||
connect(m_patternGroup, &QActionGroup::triggered, this, &FilterWidget::filterChanged);
|
connect(m_patternGroup, &QActionGroup::triggered, this, &FilterWidget::filterChanged);
|
||||||
|
|
||||||
@ -113,17 +113,17 @@ void FilterWidget::setCaseSensitivity(Qt::CaseSensitivity cs)
|
|||||||
m_caseSensitivityAction->setChecked(cs == Qt::CaseSensitive);
|
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());
|
return patternSyntaxFromAction(m_patternGroup->checkedAction());
|
||||||
}
|
}
|
||||||
|
|
||||||
void FilterWidget::setPatternSyntax(QRegExp::PatternSyntax s)
|
void FilterWidget::setPatternSyntax(PatternSyntax s)
|
||||||
{
|
{
|
||||||
const QList<QAction*> actions = m_patternGroup->actions();
|
const QList<QAction*> actions = m_patternGroup->actions();
|
||||||
for (QAction *a : actions) {
|
for (QAction *a : actions) {
|
||||||
|
@ -52,28 +52,32 @@
|
|||||||
#define FILTERWIDGET_H
|
#define FILTERWIDGET_H
|
||||||
|
|
||||||
#include <QLineEdit>
|
#include <QLineEdit>
|
||||||
#include <QRegExp>
|
|
||||||
|
|
||||||
QT_BEGIN_NAMESPACE
|
QT_BEGIN_NAMESPACE
|
||||||
class QAction;
|
class QAction;
|
||||||
class QActionGroup;
|
class QActionGroup;
|
||||||
QT_END_NAMESPACE
|
QT_END_NAMESPACE
|
||||||
|
|
||||||
Q_DECLARE_METATYPE(QRegExp::PatternSyntax)
|
|
||||||
|
|
||||||
class FilterWidget : public QLineEdit
|
class FilterWidget : public QLineEdit
|
||||||
{
|
{
|
||||||
Q_OBJECT
|
Q_OBJECT
|
||||||
Q_PROPERTY(Qt::CaseSensitivity caseSensitivity READ caseSensitivity WRITE setCaseSensitivity)
|
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:
|
public:
|
||||||
explicit FilterWidget(QWidget *parent = nullptr);
|
explicit FilterWidget(QWidget *parent = nullptr);
|
||||||
|
|
||||||
Qt::CaseSensitivity caseSensitivity() const;
|
Qt::CaseSensitivity caseSensitivity() const;
|
||||||
void setCaseSensitivity(Qt::CaseSensitivity);
|
void setCaseSensitivity(Qt::CaseSensitivity);
|
||||||
|
|
||||||
QRegExp::PatternSyntax patternSyntax() const;
|
enum PatternSyntax {
|
||||||
void setPatternSyntax(QRegExp::PatternSyntax);
|
RegularExpression,
|
||||||
|
Wildcard,
|
||||||
|
FixedString
|
||||||
|
};
|
||||||
|
Q_ENUM(PatternSyntax)
|
||||||
|
|
||||||
|
PatternSyntax patternSyntax() const;
|
||||||
|
void setPatternSyntax(PatternSyntax);
|
||||||
|
|
||||||
signals:
|
signals:
|
||||||
void filterChanged();
|
void filterChanged();
|
||||||
|
@ -83,8 +83,8 @@ bool MySortFilterProxyModel::filterAcceptsRow(int sourceRow,
|
|||||||
QModelIndex index1 = sourceModel()->index(sourceRow, 1, sourceParent);
|
QModelIndex index1 = sourceModel()->index(sourceRow, 1, sourceParent);
|
||||||
QModelIndex index2 = sourceModel()->index(sourceRow, 2, sourceParent);
|
QModelIndex index2 = sourceModel()->index(sourceRow, 2, sourceParent);
|
||||||
|
|
||||||
return (sourceModel()->data(index0).toString().contains(filterRegExp())
|
return (sourceModel()->data(index0).toString().contains(filterRegularExpression())
|
||||||
|| sourceModel()->data(index1).toString().contains(filterRegExp()))
|
|| sourceModel()->data(index1).toString().contains(filterRegularExpression()))
|
||||||
&& dateInRange(sourceModel()->data(index2).toDate());
|
&& dateInRange(sourceModel()->data(index2).toDate());
|
||||||
}
|
}
|
||||||
//! [3]
|
//! [3]
|
||||||
|
@ -148,10 +148,24 @@ void Window::setSourceModel(QAbstractItemModel *model)
|
|||||||
//! [8]
|
//! [8]
|
||||||
void Window::textFilterChanged()
|
void Window::textFilterChanged()
|
||||||
{
|
{
|
||||||
QRegExp regExp(filterWidget->text(),
|
FilterWidget::PatternSyntax s = filterWidget->patternSyntax();
|
||||||
filterWidget->caseSensitivity(),
|
QString pattern = filterWidget->text();
|
||||||
filterWidget->patternSyntax());
|
switch (s) {
|
||||||
proxyModel->setFilterRegExp(regExp);
|
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]
|
//! [8]
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user