QSortFilterProxyModel filterAcceptsRow being called unexpectedly
-
Your are accessing
items_quite a lot of time for nothing. The data method is called many times because there are lots of roles. Since you only return data for Role1 and Role2, ensure first that it's one of them and then load the data. -
Your are accessing
items_quite a lot of time for nothing. The data method is called many times because there are lots of roles. Since you only return data for Role1 and Role2, ensure first that it's one of them and then load the data.are you basically saying that I should change it to something like this?
QVariant CustomList::data(int index, int role) { QVariantMap entry; switch (role) { case myRole1: entry = items_.at(index); return QVariant::fromValue(entry["myRole1"]); case myRole2: entry = items_.at(index); return QVariant::fromValue(entry["myRole2"]); default: // should be unreachable code return QVariant(); } } -
are you basically saying that I should change it to something like this?
QVariant CustomList::data(int index, int role) { QVariantMap entry; switch (role) { case myRole1: entry = items_.at(index); return QVariant::fromValue(entry["myRole1"]); case myRole2: entry = items_.at(index); return QVariant::fromValue(entry["myRole2"]); default: // should be unreachable code return QVariant(); } } -
@wagner2x
Is that a question or an observation? Yes indeed it does, many more. Basically for most of the defined ones (what font? what color? what alignment?). @SGaist is suggesting therefore you only evaluateitems_.at(index)when absolutely necessary.If you were to have a lot of roles required in the
switchstatement one could factor out theitems_.at(index)retrieval to one place if you wish and some common expression forQVariant::fromValue(entry["myRole..."])to cut down on code if that's what you mean. -
@wagner2x
Is that a question or an observation? Yes indeed it does, many more. Basically for most of the defined ones (what font? what color? what alignment?). @SGaist is suggesting therefore you only evaluateitems_.at(index)when absolutely necessary.If you were to have a lot of roles required in the
switchstatement one could factor out theitems_.at(index)retrieval to one place if you wish and some common expression forQVariant::fromValue(entry["myRole..."])to cut down on code if that's what you mean.That was question :P I should have realized that! That makes sense. I made that minor change but still am seeing significant delays in evaluating the filter after the source model has changed.
In the original post I described Model C and Model D as QSortFilterProxyModels which basically are to distinguish Items that have been selected or not. After I set the new source, Model C and Model D both reevaluate their filter (which makes sense) but if there are many elements that pass the filter, this operations is taking a while. Any ideas why that may be? Like I meantioned...there are only 300 items in the list total.
Here is my filterAcceptsRow Function...It seems pretty straight forward but I could be doing something wrong:
bool UnselectedProxyModel::filterAcceptsRow(int source_row, const QModelIndex &source_parent) const { QModelIndex itemIndex = sourceModel()->index(source_row, 0, source_parent); if(itemIndex.isValid() && !source_parent.isValid()) { return !itemIndex.data(selected).toBool(); } return false; }Remember this takes Model E (a QIdentityProxyModel) as its source where the source of Model E is swapped in and out by user selections on the filter.
-
That was question :P I should have realized that! That makes sense. I made that minor change but still am seeing significant delays in evaluating the filter after the source model has changed.
In the original post I described Model C and Model D as QSortFilterProxyModels which basically are to distinguish Items that have been selected or not. After I set the new source, Model C and Model D both reevaluate their filter (which makes sense) but if there are many elements that pass the filter, this operations is taking a while. Any ideas why that may be? Like I meantioned...there are only 300 items in the list total.
Here is my filterAcceptsRow Function...It seems pretty straight forward but I could be doing something wrong:
bool UnselectedProxyModel::filterAcceptsRow(int source_row, const QModelIndex &source_parent) const { QModelIndex itemIndex = sourceModel()->index(source_row, 0, source_parent); if(itemIndex.isValid() && !source_parent.isValid()) { return !itemIndex.data(selected).toBool(); } return false; }Remember this takes Model E (a QIdentityProxyModel) as its source where the source of Model E is swapped in and out by user selections on the filter.
-
How does Lazy Loading work if you have a chain of models like I have? Do I need to reimplement canFetchmore and FetchMore for all of the models in the chain? Also, any ideas on what could make a ListView QML widget take forever to draw when its model gets switched?
-
I tried implementing canFetchMore and fetchMore on Models C and D but am seeing a QT error
QDeclarativeComponent: Cannot create new component instance before completing the previous
<Unknown File>: QML VisualDataModel: Error creating delegateI am trying to batch 10 delegates at a time:
bool UnselectedModel::canFetchMore(const QModelIndex &parent) const { if (parent.isValid()) return false; //I should be able to use rowCount here right? This should be executed after filteracceptsRow is finished? return (itemsFetched_ < rowCount()); } void UnselectedModel::fetchMore(const QModelIndex &parent) { if (parent.isValid()) return; int remainder = rowCount() - itemsFetched_; int itemsToFetch = qMin(10, remainder); if (itemsToFetch <= 0) return; beginInsertRows(QModelIndex(), itemsFetched_, itemsFetched_ + itemsToFetch - 1); itemsFetched_ += itemsToFetch; endInsertRows(); emit numberPopulated(itemsToFetch); }I also clear the itemsFetched_ member every time we make a sourceModel change on Model E. Do I need to reset Model C and D when the source model changes if I am implementing fetchMore?
-
Can you provide a minimal compilable example of your code ?
That would help take a better look at your situation ?