PDA

View Full Version : How to filter model from QStandardItemModel for uniqe values



Mad Bekon
14th April 2013, 22:43
Hi folks,
I'm trying to find out, which class and how should I reimplement, to get duplicate values filter.
Exactly what I want to do, is just to put uniqe values from one of QStandardItemModel columns (that contains duplicates).

Best regards,
Kris

wysota
14th April 2013, 23:56
QSortFilterProxyModel -- you'll need to subclass it and reimplement filterAcceptsRow()

Mad Bekon
15th April 2013, 00:03
Thank you. I'm examining Custom Sort/Filter example, what I believe should help me.
But I'm just wondering how filter is beeing applyed to elements. Row by row?

I plan to put all unique values into QSet, and check it they exist there. Good track?

My only concideration is, to keep original order and remove elements following after first occurance.

wysota
15th April 2013, 00:16
Row by row?
Yes.


I plan to put all unique values into QSet, and check it they exist there. Good track?
Sounds fine for a first try.

Mad Bekon
15th April 2013, 01:03
bool MySortFilterProxyModel::filterAcceptsRow(int sourceRow,
const QModelIndex &sourceParent) const
{
QModelIndex index = sourceModel()->index(sourceRow, filterKeyColumn(), sourceParent);

if(!(mySet.contains(index.data().toString()))) {

const_cast<MySortFilterProxyModel *>(this)->mySet.insert(index.data().toString());
return true;
}
else
return false;

}

Did the job... Dzięki wielkie

wysota
15th April 2013, 07:41
Remember to clear the set when the filter is invalidated.

Mad Bekon
15th April 2013, 17:42
True, true...
I cannot edit, so I will add here:


void MySortFilterProxyModel::invalidateFilter()
{
mySet.clear();
}