From a7df98a9a72ee460c6bd23388d52b1200e7ed3ba Mon Sep 17 00:00:00 2001 From: Ulf Hermann Date: Fri, 11 Oct 2019 13:39:46 +0200 Subject: QSortFilterProxyModel: Add a cheaper way to find source_sort_column There are two places where we are only interested in the mapping of proxy to source sort column, rather than the full mapping. Creating the full mapping is rather expensive as it iterates all rows and columns and allocates a large number of objects. Just figuring out the n-th accepted column can be much cheaper. Fixes: QTBUG-41659 Change-Id: I7ea914cb695518b4d47cdc3ad67c7786380d8709 Reviewed-by: David Faure Reviewed-by: Christian Ehrlicher --- src/corelib/itemmodels/qsortfilterproxymodel.cpp | 40 ++++++++++++++++++------ 1 file changed, 30 insertions(+), 10 deletions(-) (limited to 'src/corelib') diff --git a/src/corelib/itemmodels/qsortfilterproxymodel.cpp b/src/corelib/itemmodels/qsortfilterproxymodel.cpp index 675bf4b8c3..978102035e 100644 --- a/src/corelib/itemmodels/qsortfilterproxymodel.cpp +++ b/src/corelib/itemmodels/qsortfilterproxymodel.cpp @@ -377,6 +377,7 @@ public: void sort(); bool update_source_sort_column(); + int find_source_sort_column() const; void sort_source_rows(QVector &source_rows, const QModelIndex &source_parent) const; QVector > > proxy_intervals_for_source_items_to_add( @@ -479,11 +480,8 @@ void QSortFilterProxyModelPrivate::_q_clearMapping() qDeleteAll(source_index_mapping); source_index_mapping.clear(); - if (dynamic_sortfilter && update_source_sort_column()) { - //update_source_sort_column might have created wrong mapping so we have to clear it again - qDeleteAll(source_index_mapping); - source_index_mapping.clear(); - } + if (dynamic_sortfilter) + source_sort_column = find_source_sort_column(); // update the persistent indexes update_persistent_indexes(source_indexes); @@ -640,6 +638,31 @@ bool QSortFilterProxyModelPrivate::update_source_sort_column() return old_source_sort_column != source_sort_column; } +/*! + \internal + + Find the source_sort_column without creating a full mapping and + without updating anything. +*/ +int QSortFilterProxyModelPrivate::find_source_sort_column() const +{ + if (proxy_sort_column == -1) + return -1; + + const QModelIndex rootIndex; + const int source_cols = model->columnCount(); + int accepted_columns = -1; + + Q_Q(const QSortFilterProxyModel); + for (int i = 0; i < source_cols; ++i) { + if (q->filterAcceptsColumn(i, rootIndex)) { + if (++accepted_columns == proxy_sort_column) + return i; + } + } + + return -1; +} /*! \internal @@ -1591,11 +1614,8 @@ void QSortFilterProxyModelPrivate::_q_sourceLayoutChanged(const QListlayoutChanged(saved_layoutChange_parents); saved_layoutChange_parents.clear(); -- cgit v1.2.3