QSortFilterProxyModel + setFilterRegExp for more than 1 column
Hey @all,
can anybody told me a way to use one or more setFilterRegExp() calls for more than one column?
Best Regards
NoRulez
Re: QSortFilterProxyModel + setFilterRegExp for more than 1 column
same problem
I need to filter content on 2 columns.
Can I do that without extensive coding ?
Re: QSortFilterProxyModel + setFilterRegExp for more than 1 column
sortfilterproxymodel.h
Code:
#ifndef SORTFILTERPROXYMODEL_H
#define SORTFILTERPROXYMODEL_H
#include <QSortFilterProxyModel>
namespace CerSimpleDynamics
{
namespace Models
{
{
Q_OBJECT
public:
explicit SortFilterProxyModel
(QObject *parent
= 0);
void setFilterKeyColumns(const QList<qint32> &filterColumns);
void addFilterFixedString
(qint32 column,
const QString &pattern
);
protected:
bool filterAcceptsRow
(int sourceRow,
const QModelIndex &sourceParent
) const;
private:
QMap<qint32, QString> m_columnPatterns;
};
}
}
#endif // SORTFILTERPROXYMODEL_H
sortfilterproxymodel.cpp
Code:
#include "sortfilterproxymodel.h"
namespace CerSimpleDynamics
{
namespace Models
{
{
}
void SortFilterProxyModel::setFilterKeyColumns(const QList<qint32> &filterColumns)
{
m_columnPatterns.clear();
foreach(qint32 column, filterColumns)
m_columnPatterns.
insert(column,
QString());
}
void SortFilterProxyModel
::addFilterFixedString(qint32 column,
const QString &pattern
) {
if(!m_columnPatterns.contains(column))
return;
m_columnPatterns[column] = pattern;
}
bool SortFilterProxyModel
::filterAcceptsRow(int sourceRow,
const QModelIndex &sourceParent
) const {
if(m_columnPatterns.isEmpty())
return true;
bool ret = false;
for(QMap<qint32, QString>::const_iterator iter = m_columnPatterns.constBegin();
iter != m_columnPatterns.constEnd();
++iter)
{
QModelIndex index
= sourceModel
()->index
(sourceRow, iter.
key(), sourceParent
);
ret = (index.data().toString() == iter.value());
if(!ret)
return ret;
}
return ret;
}
}
}
#include "moc_sortfilterproxymodel.cpp"
Best Regards
NoRulez
Re: QSortFilterProxyModel + setFilterRegExp for more than 1 column
Re: QSortFilterProxyModel + setFilterRegExp for more than 1 column
Nevermind, I forgot this important line of code to refresh the proxy
proxyModel->invalidate();
Re: QSortFilterProxyModel + setFilterRegExp for more than 1 column
Hello,
Thank you for that code, but I have a problem When I use this code instead of a QSortFilterProxyModel data from my SourceModel (QStandardItemModel) is not shown.
Code:
proxyModel->setSourceModel(sourceModel);
sourceModel->setData(source_model->index(0, 1), "test");
qDebug()<<proxyModel->data(proxyModel->index(0,1)).toString();
With the custom SortFilterProxyModel this returns "" whereas the QSortFilterProxyModel returns "test". The defined column names from the sourceModel are adapted correctly, so the sourceModel is used, only the data is missing.
Does anyone have an idea how to solve this?
Re: QSortFilterProxyModel + setFilterRegExp for more than 1 column
Quote:
Originally Posted by
Tompazi
With the custom SortFilterProxyModel this returns "" whereas the QSortFilterProxyModel returns "test". The defined column names from the sourceModel are adapted correctly, so the sourceModel is used, only the data is missing.
Does anyone have an idea how to solve this?
Hard to tell w/o seeing your custom QSortFilterProxyModel code, but when using the QSortFilterProxyModel, your indexes through the proxy may be different than the indexes from your source model. The whole point of the QSortFilterProxyModel is that you can sort or filter items without having to change your data model items.
You should have a look at the mapFromSource method to find the proxy model index that corresponds with a source model index. If that doesn't help, please post your code for your custom QSortFilterProxyModel.
Good luck,
Jeff
Re: QSortFilterProxyModel + setFilterRegExp for more than 1 column