PDA

View Full Version : Custom Multi-Filter QSortFilterProxyModel



fruzzo
4th April 2012, 10:46
Hi,
I'm trying to develop a custom QSortFilterProxyModel (i.e. MyFilterProxyModel) supporting two kind of filters:

row filter based on "Parameter Type" throught the value returned by filterRegExp()
data filter based on "Parameter Settings Id" thorught another value returned by mySettingFilterRegExp


Following an extract of my code:



void MyFilterProxyModel::setFilteringEnabled(bool enable)
{
myFilteringEnabled = enable;
invalidateFilter();
}

bool MyFilterProxyModel::filterAcceptsRow(int sourceRow, const QModelIndex &index) const
{
if (!myFilteringEnabled)
return true;

QModelIndex sidx = sourceModel()->index(sourceRow, 0, index);
return containsParameter(sourceModel()->data(sidx, ParameterRole));
}

bool MyFilterProxyModel::containsParameter(const QVariant & variant) const
{
bool res = false;
if ( qVariantCanConvert< Parameter* >(variant) )
{
Parameter * par = qVariantValue< Parameter* >(variant);
if (par)
res = par->getType().contains(filterRegExp());
}

return res;
}

QVariant MyFilterProxyModel::data( const QModelIndex& index, int role) const
{
QVariant variant = sourceModel()->data(mapToSource(index), role);

if (role == SettingsRole)
{
if ( qVariantCanConvert< QList<Setting *> >(variant) )
{
QList<Setting *> newList;
QList<Setting *> list = qVariantValue< QList<Setting *> >(variant);
foreach(Setting * st, list)
{
bool res = st->getID().contains(mySettingFilterRegExp); //N.B. mySettingFilterRegExp value is different from the one returned by filterRegExp()
if (res)
newList.append(ev);
}

variant = qVariantFromValue(newList);
}
}

return variant;
}



The row filtering works fine but I've some problems regarding the data filtering...after I've setted mySettingFilterRegExp, the data(...) function is called only for the standard role...it isn't called for my custom roles (i.e. ParameterRole and SettingsRole). Why?

fruzzo
5th April 2012, 13:26
any idea about this problem?