I think you have to scan all the children... What you can do is implement a kind of cache - with each item associate the last searched expression and the return value for that expression, then there won't be any big overhead on the search as each item would be actually searched once and then the cached value will be returned. Pseudocode follows...
if(exp==cache(index).expression) return cache(index).value);
cache(index).expression = exp;
bool v = false;
v = itemMatchesExpression(i, exp);
if(v) break;
}
cache(index).value = v;
return cache(index).value;
}
bool Model
::filterAcceptsRow(int sourceRow,
const QModelIndex &sourceParent
) const{ return itemMatchesExpression(index(0, sourceRow, sourceParent), expression);
}
bool itemMatchesExpression(const QModelIndex &ind, const QRegExp &exp){
if(exp==cache(index).expression) return cache(index).value);
cache(index).expression = exp;
bool v = false;
foreach(QModelIndex i, ind.children()){
v = itemMatchesExpression(i, exp);
if(v) break;
}
cache(index).value = v;
return cache(index).value;
}
bool Model::filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const{
return itemMatchesExpression(index(0, sourceRow, sourceParent), expression);
}
To copy to clipboard, switch view to plain text mode
Bookmarks