How to I use filterAcceptsRow() function ?
Unsolved
General and Desktop
-
http://doc.qt.io/qt-5/qsortfilterproxymodel.html#details
It is a URL but if you read it under the sub heading "Filtering" you will see a code example.
-
Here is a sample from something I'm working on. It accepts only rows where the string _search is contained in one of the specified columns.
sample.h
class StatusFilter : public QSortFilterProxyModel { public: explicit StatusFilter(QObject * parent = 0); ~StatusFilter()= default; void setSearch(const QString &str) noexcept; private: QString _search; bool filterAcceptsRow(int source_row, const QModelIndex &) const override; };
sample.cpp
bool StatusFilter::filterAcceptsRow(int source_row, const QModelIndex &) const { QStringList sl= _search.split(QRegExp("[ ,]+"),QString::SkipEmptyParts); for (auto const &s:sl) { bool found= false; for (int col: { static_cast<int>(STATUS_COLUMN::RMA_NUMBER), static_cast<int>(STATUS_COLUMN::CUSTOMER_NAME), static_cast<int>(STATUS_COLUMN::LOCATION), static_cast<int>(STATUS_COLUMN::NOTES) }) { QModelIndex ix= sourceModel()->index(source_row,col); QString str= ix.data().toString().toLower(); if (str.contains(s)) return true; } } return false; } void StatusFilter::setSearch(const QString &str) noexcept { _search= str.toLower(); invalidate(); }
-
@mjsurette ok knk