From 6894bc0f3f8a579529dc0a7ab869ce565abd06ad Mon Sep 17 00:00:00 2001 From: Nils Jeisecke Date: Fri, 6 Dec 2013 19:00:59 +0100 Subject: Fix sorted QSortFilterProxyModel filter update When changing a filter so that a previously empty proxy model becomes populated sorting was not applied correctly. This was caused by using mapToSource for getting source_sort_column from proxy_sort_column. For an empty proxy model this won't work because no valid proxy index can be created in this case. We now directly use the root index column mapping instead by doing essentially the same as QSortFilterProxyModelPrivate::proxy_to_source but without the sanity checks needed for external use. The sorting feature of QSortFilterProxyModel has always assumed that the number of columns is specified by columnCount(QModelIndex()) so the behavior doesn't change. [ChangeLog][QtCore][QSortFilterProxyModel] Fixed sorting when a previously empty proxy model becomes populated because of a change in the filter. Task-number: QTBUG-30662 Change-Id: I21322122e127889dfadc02f838f0119ed322dcab Reviewed-by: Stephen Kelly --- src/corelib/itemmodels/qsortfilterproxymodel.cpp | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) (limited to 'src/corelib') diff --git a/src/corelib/itemmodels/qsortfilterproxymodel.cpp b/src/corelib/itemmodels/qsortfilterproxymodel.cpp index 63e0374740..930c2871d3 100644 --- a/src/corelib/itemmodels/qsortfilterproxymodel.cpp +++ b/src/corelib/itemmodels/qsortfilterproxymodel.cpp @@ -458,10 +458,21 @@ void QSortFilterProxyModelPrivate::sort() */ bool QSortFilterProxyModelPrivate::update_source_sort_column() { - Q_Q(QSortFilterProxyModel); - QModelIndex proxy_index = q->index(0, proxy_sort_column, QModelIndex()); int old_source_sort_column = source_sort_column; - source_sort_column = q->mapToSource(proxy_index).column(); + + if (proxy_sort_column == -1) { + source_sort_column = -1; + } else { + // We cannot use index mapping here because in case of a still-empty + // proxy model there's no valid proxy index we could map to source. + // So always use the root mapping directly instead. + Mapping *m = create_mapping(QModelIndex()).value(); + if (proxy_sort_column < m->source_columns.size()) + source_sort_column = m->source_columns.at(proxy_sort_column); + else + source_sort_column = -1; + } + return old_source_sort_column != source_sort_column; } -- cgit v1.2.3