diff --git a/src/app/layout/qgslayoutapputils.cpp b/src/app/layout/qgslayoutapputils.cpp index a61882b2bd0..5ab03a2e977 100644 --- a/src/app/layout/qgslayoutapputils.cpp +++ b/src/app/layout/qgslayoutapputils.cpp @@ -157,11 +157,27 @@ void QgsLayoutAppUtils::registerGuiForKnownItemTypes() QList mapItems; scalebar->layout()->layoutItems( mapItems ); - if ( !mapItems.isEmpty() ) + // try to find a good map to link the scalebar with by default + // start by trying to find a selected map + QgsLayoutItemMap *targetMap = nullptr; + for ( QgsLayoutItemMap *map : qgis::as_const( mapItems ) ) { - scalebar->setMap( mapItems.at( 0 ) ); + if ( map->isSelected() ) + { + targetMap = map; + break; + } + } + // otherwise just use first map + if ( !targetMap && !mapItems.isEmpty() ) + { + targetMap = mapItems.at( 0 ); + } + if ( targetMap ) + { + scalebar->setMap( targetMap ); + scalebar->applyDefaultSize(); } - scalebar->applyDefaultSize(); } ); registry->addLayoutItemGuiMetadata( scalebarItemMetadata.release() );