PDA

View Full Version : QSortFilterProxyModel + setFilterRegExp for more than 1 column



NoRulez
24th September 2009, 14:24
Hey @all,

can anybody told me a way to use one or more setFilterRegExp() calls for more than one column?

Best Regards
NoRulez

Exec
29th April 2010, 16:36
same problem
I need to filter content on 2 columns.
Can I do that without extensive coding ?

NoRulez
29th April 2010, 17:44
sortfilterproxymodel.h


#ifndef SORTFILTERPROXYMODEL_H
#define SORTFILTERPROXYMODEL_H

#include <QSortFilterProxyModel>

namespace CerSimpleDynamics
{
namespace Models
{
class SortFilterProxyModel : public QSortFilterProxyModel
{
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


#include "sortfilterproxymodel.h"

namespace CerSimpleDynamics
{
namespace Models
{
SortFilterProxyModel::SortFilterProxyModel(QObject *parent) : QSortFilterProxyModel(parent)
{
}

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

Maximus2
10th October 2013, 01:15
Sorry to revive this old thread.

I'm trying to use the model up here but i'm having some problem with it.
The filter is not changing my view.
Maybe I don't get something? I'm new with QT



proxyModel = new SortFilterProxyModel(this);
proxyModel->setSourceModel(tableModel);
proxyModel->setFilterCaseSensitivity(Qt::CaseInsensitive);
proxyModel->sort(0, Qt::AscendingOrder);


// -------------------- Filter here -------------------------
void MainWindow::filterWorkout() {

QString name = ui->lineEdit_nameFilter->text();
QString createdBy = ui->lineEdit_createdbyFilter->text();
qDebug() << "name=" << name;
qDebug() << "createdBy=" << createdBy;


QList<qint32> lst;
lst.append(0);
lst.append(1);
proxyModel->setFilterKeyColumns(lst);

proxyModel->addFilterFixedString(0, name);
proxyModel->addFilterFixedString(1, createdBy);

}

View still show all the member of the model, like this code did nothing..
Thanks if you can give me an hand!

Maximus2
10th October 2013, 16:02
Nevermind, I forgot this important line of code to refresh the proxy
proxyModel->invalidate();

Tompazi
21st May 2014, 22:24
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.


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?

jefftee
10th June 2014, 07:56
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 (http://qt-project.org/doc/qt-5/qabstractproxymodel.html#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

pavani_devara
21st October 2014, 07:22
Can you share the code?