5

I'm trying to use QgsProcessingException within a processing script if the output parameter is empty or corresponds to something.

In the specific case the output defined is QgsProcessingParameterFolderDestination and I would like to display the nice yellow warning in the main dialog if the path chosen is equal to a specific path and not allowing the algorithm to run.

In the processAlgorithm method I tried to use something like:

# set the output folder
Results = self.parameterAsString(parameters, self.OUTPUT, context)
if Results.startswith('/tmp'):
    raise QgsProcessingException(self.tr('Please choose another path'))

it works nice meaning that the algorithm does not start, but the warning message is written in the Log tab and not in the main dialog tab.

Python API are not so useful (https://qgis.org/pyqgis/3.0/core/Processing/QgsProcessingException.html) while C++ API are a little more verbose (https://qgis.org/api/classQgsProcessingException.html)

BTW: using QGIS master (3.1) on a linux debian machine.

To be clear, left the correct handling of the exception for a missing raster parameter, right what happens with the exception with the code of above:

enter image description here

1 Answer 1

5

You need to override QgsProccesingAlgorithm.checkParameterValues (https://qgis.org/pyqgis/3.0/core/Processing/QgsProcessingAlgorithm.html#qgis.core.QgsProcessingAlgorithm.checkParameterValues).

Return False and your error message if your custom check fails. Don't forget to call the super class method otherwise, or you'll lose all the usual parameter checks.

2
  • 1
    Thanks for the answer and hint. It works! I don't know if the following is the expected behavior: I don't have the same message bar of the default checks but a warning dialog that pops up. That's perfectly fine, just want to report this. Thanks again
    – matteo
    Commented May 31, 2018 at 6:11
  • I don't understand how this is applied. Can you give an example please?
    – Kai
    Commented Apr 22 at 0:17

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.