From c53932ff40285a8aeb187931def522bf1ca2ccae Mon Sep 17 00:00:00 2001 From: Christian Kandeler Date: Tue, 26 Jan 2021 15:31:54 +0100 Subject: [PATCH] Core: Fix crash with Search & Replace Due to the use of a proxy model, we can no longer make assumptions about the internal pointers of model indices. Amends f3d7717b31. Fixes: QTCREATORBUG-25278 Change-Id: Ia0225b0324897d16fe9650bf4282432278aeb63f Reviewed-by: Eike Ziller --- src/plugins/coreplugin/find/searchresulttreemodel.cpp | 5 +++++ src/plugins/coreplugin/find/searchresulttreemodel.h | 3 +++ src/plugins/coreplugin/find/searchresultwidget.cpp | 11 +++++------ 3 files changed, 13 insertions(+), 6 deletions(-) diff --git a/src/plugins/coreplugin/find/searchresulttreemodel.cpp b/src/plugins/coreplugin/find/searchresulttreemodel.cpp index bf9851013b2..dd4b7e42f32 100644 --- a/src/plugins/coreplugin/find/searchresulttreemodel.cpp +++ b/src/plugins/coreplugin/find/searchresulttreemodel.cpp @@ -628,6 +628,11 @@ QModelIndex SearchResultFilterModel::prev(const QModelIndex &idx, bool includeGe return sourceModel()->prev(index, includeGenerated, wrapped); }); } +SearchResultTreeItem *SearchResultFilterModel::itemForIndex(const QModelIndex &index) const +{ + return static_cast(mapToSource(index).internalPointer()); +} + bool SearchResultFilterModel::filterAcceptsRow(int source_row, const QModelIndex &source_parent) const { diff --git a/src/plugins/coreplugin/find/searchresulttreemodel.h b/src/plugins/coreplugin/find/searchresulttreemodel.h index 47e635517f1..40112930747 100644 --- a/src/plugins/coreplugin/find/searchresulttreemodel.h +++ b/src/plugins/coreplugin/find/searchresulttreemodel.h @@ -36,6 +36,7 @@ namespace Core { namespace Internal { +class SearchResultTreeItem; class SearchResultTreeModel; class SearchResultFilterModel : public QSortFilterProxyModel @@ -54,6 +55,8 @@ public: QModelIndex prev(const QModelIndex &idx, bool includeGenerated = false, bool *wrapped = nullptr) const; + SearchResultTreeItem *itemForIndex(const QModelIndex &index) const; + signals: void filterInvalidated(); diff --git a/src/plugins/coreplugin/find/searchresultwidget.cpp b/src/plugins/coreplugin/find/searchresultwidget.cpp index cfc7715a20e..4c69e5e7c76 100644 --- a/src/plugins/coreplugin/find/searchresultwidget.cpp +++ b/src/plugins/coreplugin/find/searchresultwidget.cpp @@ -535,12 +535,11 @@ QList SearchResultWidget::checkedItems() const SearchResultFilterModel *model = m_searchResultTreeView->model(); const int fileCount = model->rowCount(); for (int i = 0; i < fileCount; ++i) { - QModelIndex fileIndex = model->index(i, 0); - auto fileItem = static_cast(fileIndex.internalPointer()); - QTC_ASSERT(fileItem != nullptr, continue); - for (int rowIndex = 0; rowIndex < fileItem->childrenCount(); ++rowIndex) { - QModelIndex textIndex = model->index(rowIndex, 0, fileIndex); - auto rowItem = static_cast(textIndex.internalPointer()); + const QModelIndex fileIndex = model->index(i, 0); + const int itemCount = model->rowCount(fileIndex); + for (int rowIndex = 0; rowIndex < itemCount; ++rowIndex) { + const QModelIndex textIndex = model->index(rowIndex, 0, fileIndex); + const SearchResultTreeItem * const rowItem = model->itemForIndex(textIndex); QTC_ASSERT(rowItem != nullptr, continue); if (rowItem->checkState()) result << rowItem->item;