1

not sure if this is the expected behavior of the QgsProcessingParameterEnum. I have a very very example Processing script. Not doing anything in the processAlgorithm method because I'm focused on the initAlgorithm one:

from qgis.PyQt.QtCore import QCoreApplication
from qgis.core import (QgsProcessing,
 QgsProcessingParameterEnum,
 QgsProcessingAlgorithm,
 )
class ExampleProcessingAlgorithm2(QgsProcessingAlgorithm):
 INPUT = 'INPUT'
 def tr(self, string):
 return QCoreApplication.translate('Processing', string)
 def createInstance(self):
 return ExampleProcessingAlgorithm2()
 def name(self):
 return 'myscript2'
 def displayName(self):
 return self.tr('My Script2')
 def group(self):
 return self.tr('Example scripts 2')
 def groupId(self):
 return 'examplescripts'
 def initAlgorithm(self, config=None):
 self.addParameter(
 QgsProcessingParameterEnum(
 self.INPUT,
 self.tr('Enum'),
 allowMultiple=True
 )
 )
 def processAlgorithm(self, parameters, context, feedback):
 pass

I have to create the algorithm dialog from another file (I'm creating a plugin) that should open the algorithm dialog and fill the enum parameter with the values of the dictionary:

dialog = processing.createAlgorithmDialog(
 'script:myscript2',
 {
 'INPUT': ['a', 'b', 'c']
 }
)
dialog.exec_()

But the parameter list is empty. The reason is that I don't know in advance the values to pass to the enum parameters: ['a', 'b', 'c'] is just a silly example, in the real plugin the list with all the values is created by reading another file.

Is it possible to pass to the enum parameter a list with values not defined in the main algorithm file?

asked Jan 17, 2023 at 14:05

1 Answer 1

6

I think the intended way is to define the options in the algorithm class

def initAlgorithm(self, config=None):
 self.addParameter(
 QgsProcessingParameterEnum(
 self.INPUT,
 self.tr('Enum'),
 allowMultiple=True,
 options=['a', 'b', 'c']
 )
 )

Then you can pass the default parameter when using createAlgorithmDialog

dialog = processing.createAlgorithmDialog(
 'script:myscript2',
 {
 'INPUT': [0, 2] # Select the 1st and 3rd option
 }
)

So you will have to find another way to modify your enum options. One quick and dirty solution using QgsProcessingParameterEnum.setOptions that works in the console would be

from processing.gui.AlgorithmDialog import AlgorithmDialog
alg = QgsApplication.processingRegistry().createAlgorithmById('script:myscript2')
enum = alg.parameterDefinitions()[0]
enum.setOptions(['x', 'y', 'z'])
dlg = AlgorithmDialog(alg, parent=iface.mainWindow())
dlg.setParameters({'INPUT': [0, 2]})
dlg.exec_()
answered Mar 9, 2023 at 17:21

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.