Skip to content

Commit a33c1b1

Browse files
committed
Ensure that temporary map canvas atlas variables are never written to projects
Causes a crash on debug builds, and is generally undesirable anyway
1 parent f565d7b commit a33c1b1

File tree

2 files changed

+34
-1
lines changed

2 files changed

+34
-1
lines changed

src/gui/qgsmapcanvas.cpp

+7-1
Original file line numberDiff line numberDiff line change
@@ -2126,7 +2126,13 @@ void QgsMapCanvas::writeProject( QDomDocument &doc )
21262126

21272127
// store canvas expression context
21282128
QDomElement scopeElement = doc.createElement( QStringLiteral( "expressionContextScope" ) );
2129-
mExpressionContextScope.writeXml( scopeElement, doc, QgsReadWriteContext() );
2129+
QgsExpressionContextScope tmpScope( mExpressionContextScope );
2130+
tmpScope.removeVariable( QStringLiteral( "atlas_featurenumber" ) );
2131+
tmpScope.removeVariable( QStringLiteral( "atlas_pagename" ) );
2132+
tmpScope.removeVariable( QStringLiteral( "atlas_feature" ) );
2133+
tmpScope.removeVariable( QStringLiteral( "atlas_featureid" ) );
2134+
tmpScope.removeVariable( QStringLiteral( "atlas_geometry" ) );
2135+
tmpScope.writeXml( scopeElement, doc, QgsReadWriteContext() );
21302136
mapcanvasNode.appendChild( scopeElement );
21312137

21322138
// TODO: store only units, extent, projections, dest CRS

tests/src/python/test_qgsmapcanvas.py

+27
Original file line numberDiff line numberDiff line change
@@ -349,6 +349,33 @@ def canvasImageCheck(self, name, reference_image, canvas):
349349
print((self.report))
350350
return result
351351

352+
def testSaveCanvasVariablesToProject(self):
353+
"""
354+
Ensure that temporary canvas atlas variables are not written to project
355+
"""
356+
c1 = QgsMapCanvas()
357+
c1.setObjectName('c1')
358+
c1.expressionContextScope().setVariable('atlas_featurenumber', 1111)
359+
c1.expressionContextScope().setVariable('atlas_pagename', 'bb')
360+
c1.expressionContextScope().setVariable('atlas_feature', QgsFeature(1))
361+
c1.expressionContextScope().setVariable('atlas_featureid', 22)
362+
c1.expressionContextScope().setVariable('atlas_geometry', QgsGeometry.fromWkt('Point( 1 2 )'))
363+
c1.expressionContextScope().setVariable('vara', 1111)
364+
c1.expressionContextScope().setVariable('varb', 'bb')
365+
366+
doc = QDomDocument("testdoc")
367+
elem = doc.createElement("qgis")
368+
doc.appendChild(elem)
369+
c1.writeProject(doc)
370+
371+
c2 = QgsMapCanvas()
372+
c2.setObjectName('c1')
373+
c2.readProject(doc)
374+
375+
self.assertCountEqual(c2.expressionContextScope().variableNames(), ['vara', 'varb'])
376+
self.assertEqual(c2.expressionContextScope().variable('vara'), 1111)
377+
self.assertEqual(c2.expressionContextScope().variable('varb'), 'bb')
378+
352379
def testSaveMultipleCanvasesToProject(self):
353380
# test saving/restoring canvas state to project with multiple canvases
354381
c1 = QgsMapCanvas()

0 commit comments

Comments
 (0)