Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Defer calling postProcessors until layers have been added to layer tree
It's possible that an algorithm's postProcessor may contain logic
relating to the layer tree, so make sure that all layers have
already been added to the tree before calling postProcessors
  • Loading branch information
nyalldawson committed May 10, 2023
1 parent f27195c commit c32bc17
Showing 1 changed file with 15 additions and 4 deletions.
19 changes: 15 additions & 4 deletions python/plugins/processing/gui/Postprocessing.py
Expand Up @@ -198,6 +198,8 @@ def handleAlgorithmResults(alg: QgsProcessingAlgorithm,
i = 0

added_layers: List[Tuple[QgsLayerTreeGroup, QgsLayerTreeLayer]] = []
layers_to_post_process: List[Tuple[QgsMapLayer,
QgsProcessingContext.LayerDetails]] = []

for dest_id, details in context.layersToLoadOnCompletion().items():
if feedback.isCanceled():
Expand Down Expand Up @@ -235,10 +237,11 @@ def handleAlgorithmResults(alg: QgsProcessingAlgorithm,
added_layers.append((results_group, layer_tree_layer))

if details.postProcessor():
details.postProcessor().postProcessLayer(
layer,
context,
feedback)
# we defer calling the postProcessor set in the context
# until the layer has been added to the project's layer
# tree, just in case the postProcessor contains logic
# relating to layer tree handling
layers_to_post_process.append((layer, details))

else:
wrong_layers.append(str(dest_id))
Expand All @@ -262,6 +265,14 @@ def handleAlgorithmResults(alg: QgsProcessingAlgorithm,
layer_node.removeCustomProperty(SORT_ORDER_CUSTOM_PROPERTY)
group.insertChildNode(0, layer_node)

# all layers have been added to the layer tree, so safe to call
# postProcessors now
for layer, details in layers_to_post_process:
details.postProcessor().postProcessLayer(
layer,
context,
feedback)

feedback.setProgress(100)

if wrong_layers:
Expand Down

0 comments on commit c32bc17

Please sign in to comment.