1

In a custom plugin I'm calling some custom QgsProcessingAlgorithm that are loaded with the plugin itself.

If I create an instance of these algorithms I need to prefill the parameters with some custom values and I think this can easily be done by passing a custom dictionary to the initAlgorithm method [0].

I'm setting some side variables needed by be plugin (that have not to be outputs), but again this can be done by setting some class variables (i.e. self.my_custom_id = 'zzz') and getting them when needed.

my_algorithm = MyCustomAlgorithm()
my_algorithm.initAlgorithm(
 'INPUT': 'custom_path'
)
# how to open and exec the algorithm dialog?

What I'm not able to do is to simply open/exec the algorithm dialog.

If I use processing.createAlgorithmDialog then I can exec the algorithm dialog but I cannot retrieve the side variables needed.

my_dialog = processing.createAlgorithmDialog(
 'my_custom_provider:my_custom_algorithm`,
 {
 'INPUT': 'custom_path'
 }
)
my_dialog.exec_()

I'm stuck in this loop. Somebody has already faced the same issue?

[0] https://qgis.org/pyqgis/master/core/QgsProcessingAlgorithm.html#qgis.core.QgsProcessingAlgorithm.initAlgorithm

PolyGeo
65.5k29 gold badges115 silver badges350 bronze badges
asked Aug 1, 2022 at 10:03

1 Answer 1

2

In your second approach you could use the algorithm() method to retrieve the instance of your custom QgsProcessingAlgorithm and then access its class variables.

my_dialog = processing.createAlgorithmDialog(
 'my_custom_provider:my_custom_algorithm',
 {
 'INPUT': 'custom_path'
 }
)
# access your custom class variables
print(my_dialog.algorithm().my_custom_id) # prints 'zzz'
my_dialog.exec()

createAlgorithmDialog can also receive an instance of QgsProcessingAlgorithm as first argument to create a dialog. Therefore the following will also be a valid option:

my_algorithm = MyCustomAlgorithm()
my_dialog = processing.createAlgorithmDialog(
 my_algorithm,
 {
 'INPUT': 'custom_path'
 }
)
# access your custom class variables
print(my_algorithm.my_custom_id) # prints 'zzz'
# execute dialog
my_dialog.exec()
answered Aug 2, 2022 at 11:37
1
  • thanks! No idea why but my QGIS (doesn't matter which version 3.22, 3.26, 3.27) is crashing if I make an instance of my algorithm and pass it to the createAlgorithmDialog method. Anyway, I can use the unique identifier. I also discovered that you have access to the resuls with my_dialog.results() after the execution. Commented Aug 2, 2022 at 15:42

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.