Skip to content

Commit cfc5b37

Browse files
committedNov 21, 2020
Fix unreported crash when removing items from layout legend
Master only
1 parent 41dc29a commit cfc5b37

File tree

1 file changed

+13
-11
lines changed

1 file changed

+13
-11
lines changed
 

‎src/gui/layout/qgslayoutlegendwidget.cpp

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -939,16 +939,16 @@ void QgsLayoutLegendWidget::mRemoveToolButton_clicked()
939939

940940
mLegend->beginCommand( tr( "Remove Legend Item" ) );
941941

942-
QList<QPersistentModelIndex> indexes;
943-
const auto constSelectedIndexes = selectionModel->selectedIndexes();
944-
for ( const QModelIndex &index : constSelectedIndexes )
945-
indexes << index;
942+
QList<QPersistentModelIndex> proxyIndexes;
943+
const QModelIndexList viewSelection = selectionModel->selectedIndexes();
944+
for ( const QModelIndex &index : viewSelection )
945+
proxyIndexes << index;
946946

947947
// first try to remove legend nodes
948948
QHash<QgsLayerTreeLayer *, QList<int> > nodesWithRemoval;
949-
for ( const QPersistentModelIndex &index : qgis::as_const( indexes ) )
949+
for ( const QPersistentModelIndex &proxyIndex : qgis::as_const( proxyIndexes ) )
950950
{
951-
if ( QgsLayerTreeModelLegendNode *legendNode = mItemTreeView->index2legendNode( index ) )
951+
if ( QgsLayerTreeModelLegendNode *legendNode = mItemTreeView->index2legendNode( proxyIndex ) )
952952
{
953953
QgsLayerTreeLayer *nodeLayer = legendNode->layerNode();
954954
nodesWithRemoval[nodeLayer].append( _unfilteredLegendNodeIndex( legendNode ) );
@@ -960,8 +960,7 @@ void QgsLayoutLegendWidget::mRemoveToolButton_clicked()
960960
std::sort( toDelete.begin(), toDelete.end(), std::greater<int>() );
961961
QList<int> order = QgsMapLayerLegendUtils::legendNodeOrder( it.key() );
962962

963-
const auto constToDelete = toDelete;
964-
for ( int i : constToDelete )
963+
for ( int i : qgis::as_const( toDelete ) )
965964
{
966965
if ( i >= 0 && i < order.count() )
967966
order.removeAt( i );
@@ -972,10 +971,13 @@ void QgsLayoutLegendWidget::mRemoveToolButton_clicked()
972971
}
973972

974973
// then remove layer tree nodes
975-
for ( const QPersistentModelIndex &index : qgis::as_const( indexes ) )
974+
for ( const QPersistentModelIndex &proxyIndex : qgis::as_const( proxyIndexes ) )
976975
{
977-
if ( index.isValid() && mItemTreeView->index2node( index ) )
978-
mLegend->model()->removeRow( index.row(), index.parent() );
976+
if ( proxyIndex.isValid() && mItemTreeView->index2node( proxyIndex ) )
977+
{
978+
const QModelIndex sourceIndex = mItemTreeView->proxyModel()->mapToSource( proxyIndex );
979+
mLegend->model()->removeRow( sourceIndex.row(), sourceIndex.parent() );
980+
}
979981
}
980982

981983
mLegend->updateLegend();

0 commit comments

Comments
 (0)
Please sign in to comment.