PDA

View Full Version : QSortFilterProxyModel.invalidateFilter() misunderstanding



aspidites
23rd April 2009, 05:43
The Long Version
I have a QSortFilterProxyModel subclass that reimplements filterAcceptsRow(). In filterAcceptsRow, I check the result of various regexp's (one for each column of a tableview). Additionally, I have numerous set methods that change the value of the regexp patterns used in filterAcceptsRow() and call invalidateFilter().

In my main window, I have a QLineEdit that, upon emitting its textChanged signal sets the forementioned filters according to a specific syntax. If, for example, I have:

name=beagle; type=dog

the result set would consist of rows who's name column contains beagle and type column contains dog.

What I can't figure out, is how to remove all filters if the line edit is empty. Well, I can, but I can't help but think there must be a more correct way to do it. Here is some pseudo-code (python) that demonstrates what I've tried, and what I've ended up doing until I find a more permanent solution:



if len(lineEdit.text()) > 0:
field = lineEdit.text().split("=")[0]
pattern = lineEdit.text().split("=")[1]
if field == name:
model.setNameFilter(pattern)
elif field == type:
model.setTypeFilter(pattern)
else:
model.invalidateFilter() # doesn't work
model.clear() # also doesn't work
model.invalidate() # again, doesn't work
model.setNameFilter("") # works


The Short Version
In short, what method should I be calling to get the model back to it's original state? Have I misunderstood the use of invalidateFilter? Should I be reimplementing it?

Thanks in advance for any help. I've been teaching myself python and Qt for the last month or so, and things have been fairly smooth up until now.

janus
23rd April 2009, 08:20
Hi,

I am not sure if I fully understand the problem but maybe you could call the base class if the filters are empty e.g:


if (nameFilter.isEmpty() && typeFilter.isEmpty())
QSortFilterProxyModel::filterAcceptsRow(source_row , source_parent )

aspidites
23rd April 2009, 13:17
That's not quite what I'm trying to check for. Instead of checking for emtpy filters, I'm trying to check for an empty widget (line edit).

What I'd lik to do is remove all filters from the model if the line edit is empty. In other words, if it is empty, all filters should be cleared. I'd like to clear all the filters without having to set them to "" like this:



self.setNameFilter("")
self.setTypeFilter("")


Failing that, I'd like to at least find a way to ignore their values and return the model to its original state.

I'm going to have another look at the base class's implementation of reset and invalidate(). Thanks again.