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 <eike.ziller@qt.io>
This commit is contained in:
Christian Kandeler
2021-01-26 15:31:54 +01:00
parent ab8f26607d
commit c53932ff40
3 changed files with 13 additions and 6 deletions

View File

@@ -628,6 +628,11 @@ QModelIndex SearchResultFilterModel::prev(const QModelIndex &idx, bool includeGe
return sourceModel()->prev(index, includeGenerated, wrapped); }); return sourceModel()->prev(index, includeGenerated, wrapped); });
} }
SearchResultTreeItem *SearchResultFilterModel::itemForIndex(const QModelIndex &index) const
{
return static_cast<SearchResultTreeItem *>(mapToSource(index).internalPointer());
}
bool SearchResultFilterModel::filterAcceptsRow(int source_row, bool SearchResultFilterModel::filterAcceptsRow(int source_row,
const QModelIndex &source_parent) const const QModelIndex &source_parent) const
{ {

View File

@@ -36,6 +36,7 @@
namespace Core { namespace Core {
namespace Internal { namespace Internal {
class SearchResultTreeItem;
class SearchResultTreeModel; class SearchResultTreeModel;
class SearchResultFilterModel : public QSortFilterProxyModel class SearchResultFilterModel : public QSortFilterProxyModel
@@ -54,6 +55,8 @@ public:
QModelIndex prev(const QModelIndex &idx, bool includeGenerated = false, QModelIndex prev(const QModelIndex &idx, bool includeGenerated = false,
bool *wrapped = nullptr) const; bool *wrapped = nullptr) const;
SearchResultTreeItem *itemForIndex(const QModelIndex &index) const;
signals: signals:
void filterInvalidated(); void filterInvalidated();

View File

@@ -535,12 +535,11 @@ QList<SearchResultItem> SearchResultWidget::checkedItems() const
SearchResultFilterModel *model = m_searchResultTreeView->model(); SearchResultFilterModel *model = m_searchResultTreeView->model();
const int fileCount = model->rowCount(); const int fileCount = model->rowCount();
for (int i = 0; i < fileCount; ++i) { for (int i = 0; i < fileCount; ++i) {
QModelIndex fileIndex = model->index(i, 0); const QModelIndex fileIndex = model->index(i, 0);
auto fileItem = static_cast<SearchResultTreeItem *>(fileIndex.internalPointer()); const int itemCount = model->rowCount(fileIndex);
QTC_ASSERT(fileItem != nullptr, continue); for (int rowIndex = 0; rowIndex < itemCount; ++rowIndex) {
for (int rowIndex = 0; rowIndex < fileItem->childrenCount(); ++rowIndex) { const QModelIndex textIndex = model->index(rowIndex, 0, fileIndex);
QModelIndex textIndex = model->index(rowIndex, 0, fileIndex); const SearchResultTreeItem * const rowItem = model->itemForIndex(textIndex);
auto rowItem = static_cast<SearchResultTreeItem *>(textIndex.internalPointer());
QTC_ASSERT(rowItem != nullptr, continue); QTC_ASSERT(rowItem != nullptr, continue);
if (rowItem->checkState()) if (rowItem->checkState())
result << rowItem->item; result << rowItem->item;