From e7f76a6f2656ba4406efa75377a47aae60d7f569 Mon Sep 17 00:00:00 2001 From: visualfc Date: Mon, 7 Sep 2026 09:13:31 +0800 Subject: [PATCH] fix folder tree crash during git checkout --- .../src/utils/folderview/multiindexmodel.cpp | 29 ++++++++++++++----- .../src/utils/folderview/multiindexmodel_p.h | 3 +- 2 files changed, 22 insertions(+), 10 deletions(-) diff --git a/liteidex/src/utils/folderview/multiindexmodel.cpp b/liteidex/src/utils/folderview/multiindexmodel.cpp index 5c548b099..dd1f8e41d 100644 --- a/liteidex/src/utils/folderview/multiindexmodel.cpp +++ b/liteidex/src/utils/folderview/multiindexmodel.cpp @@ -123,7 +123,14 @@ QModelIndexPairList MultiIndexModelPrivate::store_persistent_indexes(const QMode QModelIndexPairList source_indexes; foreach (QModelIndex proxy_index, persistentList) { SourceModelIndex index = q->mapToSourceEx(proxy_index); - source_indexes.append(qMakePair(proxy_index, QPersistentModelIndex(index.index))); + // A source model may be rebuilding its index tree while emitting a + // layout change. Keep invalid mappings invalid instead of asking it + // to create an index from stale mapping data. + if (index.model == 0 || !index.index.isValid()) { + source_indexes.append(qMakePair(proxy_index, QPersistentModelIndex())); + } else { + source_indexes.append(qMakePair(proxy_index, QPersistentModelIndex(index.index))); + } } return source_indexes; } @@ -499,11 +506,18 @@ SourceModelIndex MultiIndexModel::mapToSourceEx(const QModelIndex &proxyIndex) c Mapping *m = (Mapping*)proxyIndex.internalPointer(); if (m == 0) { + if (proxyIndex.row() < 0 || proxyIndex.row() >= d->indexList.size()) { + return SourceModelIndex(); + } SourceModelIndex si = d->indexList[proxyIndex.row()]; + if (si.model == 0 || !si.index.isValid()) { + return SourceModelIndex(); + } si.index = si.index.sibling(si.index.row(),proxyIndex.column()); return si; } - if (m->source_rows.size() <= proxyIndex.row()) { + if (m->sourceModel == 0 || proxyIndex.row() < 0 || + m->source_rows.size() <= proxyIndex.row()) { return SourceModelIndex(); } @@ -937,14 +951,13 @@ void MultiIndexModelPrivate::_q_sourceLayoutAboutToBeChanged() // q->layoutAboutToBeChanged(); Q_Q(MultiIndexModel); saved_persistent_indexes.clear(); - emit q->layoutAboutToBeChanged(); QModelIndexList persistentList = q->persistentIndexList(); - if (persistentList.isEmpty()) - return; + if (!persistentList.isEmpty()) + saved_persistent_indexes = store_persistent_indexes(persistentList); -// QAbstractItemModel *model = (QAbstractItemModel*)sender(); -// qDebug() << "begin changed" << model; - saved_persistent_indexes = store_persistent_indexes(persistentList); + // Save indexes before notifying observers. Views may synchronously query + // the proxy while the source model is in its layout-about-to-change phase. + emit q->layoutAboutToBeChanged(); } void MultiIndexModelPrivate::_q_sourceLayoutChanged() diff --git a/liteidex/src/utils/folderview/multiindexmodel_p.h b/liteidex/src/utils/folderview/multiindexmodel_p.h index f9659c306..f45f3eec7 100644 --- a/liteidex/src/utils/folderview/multiindexmodel_p.h +++ b/liteidex/src/utils/folderview/multiindexmodel_p.h @@ -38,7 +38,7 @@ struct Mapping int rowCount; QVector source_rows; //source rows QVector proxy_rows; //proxy rows - QModelIndex sourceParent; //source parent + QPersistentModelIndex sourceParent; //source parent QAbstractItemModel *sourceModel; }; @@ -118,4 +118,3 @@ public slots: }; #endif // MULTIINDEXMODEL_P_H -