filterAcceptsRow from QSortFilterProxyModel is not called when item state of source model changed
-
@Christian-Ehrlicher I just created example based on
QStandardItemModel()
. It just beacause my real model depends on external library. But anyway this example could help me to understand how to "connect" checked items with proxy model
Here is the zipped project:
https://yadi.sk/d/Kft2dNpTEuTEeQ -
Your testcase is wrong - you can't simply cast the internal pointer to a QStandardItem. You need
auto sm = static_cast<QStandardItemModel*>(sourceModel());
QStandardItem* item = sm->itemFromIndex(index);Then it works as expected. The only thing is that the children are only visible when the parent was checked once but this is an implementation problem in your filterAcceptsRow() function.
btw: setDynamicSortFilter() is not needed as I saw now
-
@Christian-Ehrlicher thank you!
Now I'm trying to overcome the thing that children are not displayed if their parent is not checked -
@Please_Help_me_D You have to adjust your filterAcceptRow function to check for the children if one is checked.
-
@Christian-Ehrlicher
filterAcceptsRow
doesn't get called when I check the child from unchecked parent. And when I launch the app all children are UnChecked.
Or I misunderstand something?
On the picture I checked a child butfilterAcceptsRow
was not called
-
I just understood that
filterAcceptsRow
get invoked only if parent ofindex
is displayed in proxy model. Oherwise proxy model thinks that it is not worth to insert row in proxy model.
I can't understand what method ofQSortFilterProxyModel
I should rewrite to make the proxy to add each checked items regardless of its parent is shown or not.
Does anybody know? -
@Please_Help_me_D said in filterAcceptsRow from QSortFilterProxyModel is not called when item state of source model changed:
Does anybody know?
You can emit a dataChanged() signal for the parents to trigger a recheck.
-
I just understood that filterAcceptsRow get invoked only if parent of index is displayed in proxy model
I truly do not know if the following has any relevance to your situation. But it only takes 5 seconds to try, and you can throw away if it makes no difference.
Qt introduced https://doc.qt.io/qt-5/qsortfilterproxymodel.html#recursiveFilteringEnabled-prop. The default is false. Try setting it to true and see whether that causes it to evaluate
filterAcceptsRow()
against whatever node you are saying it does not currently look at? -
@Christian-Ehrlicher I'm thinking about this but I'm trying to avoid sending items that were not changed via dataChanged() signal.
@JonB Yes I've already tried it but this doesn't solve my task. It slightly changes the behaviour of proxy model but not in a way I expect it to be -
I decided to make the following:
I'm going to make all the items (except the root) checkable. When I change checkstate of a item toQt::Checked
then this item also changes checkState of its parent toQt::PartiallyChecked
.
So this chain should work but I have not finished it yet