I have a custom subclass of QSortFilterProxyModel
. I overrode filterAcceptsRow
with the custom filter I wanted. Then I used it as such:
proxy = new MyFilterModel();
proxy->setSourceModel(...);
proxy->setDynamicSortFilter(true);
proxy->setFilterParams(...); // my custom function
comboBox->setModel(proxy);
However, when the underlying source model updated such that rows that previously were filtered should no longer be filtered, the combo box was not updated with those rows. Why is that?
Eventually I overrode setSourceModel
in MyFilterModel
like so:
void MyFilterModel::setSourceModel(QAbstractItemModel *sourceModel)
{
QSortFilterProxyModel::setSourceModel(sourceModel);
connect(sourceModel, SIGNAL(dataChanged(const QModelIndex&, const QModelIndex&)), this, SLOT(doInvalidateFilter()));
connect(sourceModel, SIGNAL(modelReset()), this, SLOT(doInvalidateFilter()));
invalidateFilter();
}
... where all doInvalidateFilter()
does is call invalidateFilter
. This worked - now when the model updated, the filter rows themselves also updated.
I figure that setDynamicSortFilter
should have already taken care of this, though. Is this a bug in Qt?
dataChanged
in your sorce model. I propose you to setdynamicSortFilter
property to false and callinvalidate()
directly. – OrgeatdataChanged
to the slot that invalidates the filter) – Haynesmain.cpp
that has a single#include <QtWidgets>
at the beginning, and#include "main.moc"
at the end if you have anyQ_OBJECT
macros in the file. – Topping