@@ -1155,18 +1155,27 @@ QgsExpressionContext QgsLayoutItemMap::createExpressionContext() const
1155
1155
scope->addVariable ( QgsExpressionContextScope::StaticVariable ( QStringLiteral ( " map_crs_definition" ), mapCrs.toProj4 (), true ) );
1156
1156
scope->addVariable ( QgsExpressionContextScope::StaticVariable ( QStringLiteral ( " map_units" ), QgsUnitTypes::toString ( mapCrs.mapUnits () ), true ) );
1157
1157
1158
- QVariantList layers_ids;
1159
- scope->addVariable ( QgsExpressionContextScope::StaticVariable ( QStringLiteral ( " map_layers_ids" ), layers_ids, true ) );
1158
+ QVariantList layersIds;
1159
+ QVariantList layers;
1160
+ scope->addVariable ( QgsExpressionContextScope::StaticVariable ( QStringLiteral ( " map_layers_ids" ), layersIds, true ) );
1161
+ scope->addVariable ( QgsExpressionContextScope::StaticVariable ( QStringLiteral ( " map_layers" ), layers, true ) );
1160
1162
1161
1163
context.appendScope ( scope );
1162
1164
1163
- // The scope map_layers_ids has been added to the context, only now we can call layersToRender
1165
+ // The scope map_layers_ids and map_layers variables have been added to the context, only now we can
1166
+ // call layersToRender (just in case layersToRender relies on evaluating an expression which uses
1167
+ // other variables contained within the map settings scope
1164
1168
const QList<QgsMapLayer *> layersInMap = layersToRender ( &context );
1169
+
1170
+ layersIds.reserve ( layersInMap.count () );
1171
+ layers.reserve ( layersInMap.count () );
1165
1172
for ( QgsMapLayer *layer : layersInMap )
1166
1173
{
1167
- layers_ids << layer->id ();
1174
+ layersIds << layer->id ();
1175
+ layers << QVariant::fromValue<QgsWeakMapLayerPointer>( QgsWeakMapLayerPointer ( layer ) );
1168
1176
}
1169
- scope->setVariable ( QStringLiteral ( " map_layers_ids" ), layers_ids );
1177
+ scope->addVariable ( QgsExpressionContextScope::StaticVariable ( QStringLiteral ( " map_layers_ids" ), layersIds, true ) );
1178
+ scope->addVariable ( QgsExpressionContextScope::StaticVariable ( QStringLiteral ( " map_layers" ), layers, true ) );
1170
1179
1171
1180
return context;
1172
1181
}
0 commit comments