PDA

View Full Version : Filter Proxy Model to Autoupdate View



Big Duck
1st June 2006, 19:48
Hi,
I have subclassed the SortFilterProxyModel to provide custom filtering of data.
The filtering works but I can't get the View to update with the new filters, until the user clicks on a header to sort.

Anyone know how to update a view with a Filter Proxy automatically.
TableView.update() doesnt work for me.
I have probably miss understood the Model/View Concepts somewhere.
Or have failed to find right commands in the documentation.

Any help appreciated!

EDIT: I got it working, but I'm really not sure if what I'm doing is a bit hackish or not.




MySortFilterProxyModel *myProxyModel;

MySortFilterProxyModel::MySortFilterProxyModel(QOb ject *parent)
: QSortFilterProxyModel(parent)
{
}

bool MySortFilterProxyModel::filterAcceptsRow(int sourceRow,
const QModelIndex &sourceParent) const
{
QModelIndex index1 = sourceModel()->index(sourceRow, 1, sourceParent);
QModelIndex index2 = sourceModel()->index(sourceRow, 2, sourceParent);
QModelIndex index3 = sourceModel()->index(sourceRow, 3, sourceParent);
QModelIndex index4 = sourceModel()->index(sourceRow, 4, sourceParent);
QModelIndex index5 = sourceModel()->index(sourceRow, 5, sourceParent);

return (sourceModel()->data(index1).toString().contains(filterPlayerWhite )
&& sourceModel()->data(index2).toString().contains(filterRankWhite)
&& sourceModel()->data(index3).toString().contains(filterPlayerBlack )
&& sourceModel()->data(index4).toString().contains(filterRankBlack)
&& sourceModel()->data(index5).toString().contains(filterResult));
}

connect(filters->lineEditPlayerWhite, SIGNAL(textChanged(const QString &)),
this, SLOT(doFilter()));

void MainWindow::doFilter()
{
myProxyModel->filterPlayerWhite = lineEditPlayerWhite->text();
// NEED TO UPDATE VIEW NOW

// THESE two lines work, but is it really necessary to setup these connections again. Just to display these filters right now?
myProxyModel->setSourceModel(model);
treeView->setModel(myProxyModel);
}

jacek
1st June 2006, 20:32
IMO, you need something like:
void MySortFilterProxyModel::setWhitePlayerFilter( const QString& filter )
{
filterPlayerWhite = filter;
QModelIndex topLeft( index( 0, <column> ) );
QModelIndex bottomRight( index( rowCount() - 1, <column> ) );
emit dataChanged( topLeft, bottomRight );
}