QSortFilterProxyModel no relaying the dataChanged signal

3.3k views Asked by At

I have a custom model which append to emit the dataChanged() signal from time to time when some cell's values are changed. This works great on its own, and the view is updated as needed.

But then I tried to insert a QSortFilterProxyModel in the middle to allow sorting (no filtering needed) on my view, and apparently the dataChanged() signal is not relayed through the proxy model.

As far as I can tell, it's the expected behaviour for QSortFilterProxyModel, is there any solution other than subclassing QSortFilterProxyModel, just to add a slot to relay the dataChanged() signal and mapping items accordingly?

In my case it's pretty straight forward as only a single cell is updated at a time, or a whole column, but nothing else funky, so mapping indexes should be pretty simple...

2

There are 2 answers

0
Nightmare On

You can invoke signal from QSortFilterProxyModel without inheriting it by QMetoObject::invokeMethod Dont forget to check helper functions: mapFromSource mapToSource

0
v.shashenko On

QSortFilterProxyModel is supposed to emit the dataChanged() signal like a normal model would do, so the view will get updated. We can see in Qt's source code (qsortfilterproxymodel.cpp) that QSortFilterProxyModel::setSourceModel attaches a handler to the dataChanged() signal of the source model. The handler does its work and emits QSortFilterProxyModel's dataChanged() signal if everything went well. Note that it has a number of checks that can prevent the signal from being emitted.

If that doesn't happen, make sure that your custom model is implemented correctly, otherwise QSortFilterProxyModel logic can suppress the signal. For example, when I had the same problem it turned out that my implementation of QAbstractItemModel::parent method was wrong.

In the worst case you can try to compile Qt yourself and do some debugging to find our the reason.