Skip to content

Commit

Permalink
Brush up the basicsortfiltermodel example
Browse files Browse the repository at this point in the history
- Add a clear button to the line edit
- Indicate invalid regular expressions

Change-Id: I1dbeaa0f9168224ccb9134c0c1fe281da14dcbce
Reviewed-by: Shawn Rutledge <[email protected]>
  • Loading branch information
FriedemannKleint committed Oct 14, 2020
1 parent b0cd3bc commit d55940d
Showing 1 changed file with 25 additions and 1 deletion.
26 changes: 25 additions & 1 deletion examples/widgets/itemviews/basicsortfiltermodel/window.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,20 @@

#include "window.h"

static inline QColor textColor(const QPalette &palette)
{
return palette.color(QPalette::Active, QPalette::Text);
}

static void setTextColor(QWidget *w, const QColor &c)
{
auto palette = w->palette();
if (textColor(palette) != c) {
palette.setColor(QPalette::Active, QPalette::Text, c);
w->setPalette(palette);
}
}

Window::Window()
{
proxyModel = new QSortFilterProxyModel;
Expand All @@ -70,6 +84,7 @@ Window::Window()
filterCaseSensitivityCheckBox = new QCheckBox(tr("Case sensitive filter"));

filterPatternLineEdit = new QLineEdit;
filterPatternLineEdit->setClearButtonEnabled(true);
filterPatternLabel = new QLabel(tr("&Filter pattern:"));
filterPatternLabel->setBuddy(filterPatternLineEdit);

Expand Down Expand Up @@ -160,7 +175,16 @@ void Window::filterRegularExpressionChanged()
if (!filterCaseSensitivityCheckBox->isChecked())
options |= QRegularExpression::CaseInsensitiveOption;
QRegularExpression regularExpression(pattern, options);
proxyModel->setFilterRegularExpression(regularExpression);

if (regularExpression.isValid()) {
filterPatternLineEdit->setToolTip(QString());
proxyModel->setFilterRegularExpression(regularExpression);
setTextColor(filterPatternLineEdit, textColor(style()->standardPalette()));
} else {
filterPatternLineEdit->setToolTip(regularExpression.errorString());
proxyModel->setFilterRegularExpression(QRegularExpression());
setTextColor(filterPatternLineEdit, Qt::red);
}
}

void Window::filterColumnChanged()
Expand Down

0 comments on commit d55940d

Please sign in to comment.