@@ -939,16 +939,16 @@ void QgsLayoutLegendWidget::mRemoveToolButton_clicked()
939
939
940
940
mLegend ->beginCommand ( tr ( " Remove Legend Item" ) );
941
941
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;
946
946
947
947
// first try to remove legend nodes
948
948
QHash<QgsLayerTreeLayer *, QList<int > > nodesWithRemoval;
949
- for ( const QPersistentModelIndex &index : qgis::as_const ( indexes ) )
949
+ for ( const QPersistentModelIndex &proxyIndex : qgis::as_const ( proxyIndexes ) )
950
950
{
951
- if ( QgsLayerTreeModelLegendNode *legendNode = mItemTreeView ->index2legendNode ( index ) )
951
+ if ( QgsLayerTreeModelLegendNode *legendNode = mItemTreeView ->index2legendNode ( proxyIndex ) )
952
952
{
953
953
QgsLayerTreeLayer *nodeLayer = legendNode->layerNode ();
954
954
nodesWithRemoval[nodeLayer].append ( _unfilteredLegendNodeIndex ( legendNode ) );
@@ -960,8 +960,7 @@ void QgsLayoutLegendWidget::mRemoveToolButton_clicked()
960
960
std::sort ( toDelete.begin (), toDelete.end (), std::greater<int >() );
961
961
QList<int > order = QgsMapLayerLegendUtils::legendNodeOrder ( it.key () );
962
962
963
- const auto constToDelete = toDelete;
964
- for ( int i : constToDelete )
963
+ for ( int i : qgis::as_const ( toDelete ) )
965
964
{
966
965
if ( i >= 0 && i < order.count () )
967
966
order.removeAt ( i );
@@ -972,10 +971,13 @@ void QgsLayoutLegendWidget::mRemoveToolButton_clicked()
972
971
}
973
972
974
973
// then remove layer tree nodes
975
- for ( const QPersistentModelIndex &index : qgis::as_const ( indexes ) )
974
+ for ( const QPersistentModelIndex &proxyIndex : qgis::as_const ( proxyIndexes ) )
976
975
{
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
+ }
979
981
}
980
982
981
983
mLegend ->updateLegend ();
0 commit comments