2

There is an option, in the graphical modeler of QGIS 3.10, to mark input fields as "advanced" to hide them from user by default (advanced model parameters). I wonder how to achieve it from a Python script? I have tried to create a model from the graphical modeler and then convert it to a python script but it's not working (no "advanced" fields are marked in any way). I thought the addParameter() method of QgsProcessingAlgorithm class should have an additional parameter but it's not.

Is there a different way to create a collapsable box of advanced parameters in a processing algorithm window?

2

Indeed, it seems the "Export to Python" doesn't follow the advanced setting. You need to adapt the code a little bit.

When you have a new parameter like in the exported model:

self.addParameter(QgsProcessingParameterString(your python code))

You need to update to:

parameter = QgsProcessingParameterString(your python code)
parameter.setFlags(parameter.flags() | QgsProcessingParameterDefinition.FlagAdvanced)
self.addParameter(parameter)

And also you need to import on top of the file:

from qgis.core import QgsProcessingParameterDefinition

I think you can create a ticket saying that "Export to python doesn't follow the "Advanced" panel settings.

Side note, as this advanced setting is new in QGIS 3.10, better check if the model can still run on older version of QGIS. (> 3.4)

  • It works also in QGIS 3.4. – Andrzej Nov 1 '19 at 16:03
  • Well, Python model export came in QGIS 3.6 and they had to tweak a little bit the python code, so it's not 100% sure. Especially if you use new parameter or new algorithm which weren't in 3.4. – etrimaille Nov 1 '19 at 17:16
  • I know that there have been many changes in subsequent versions of QGIS. I have a model working under the version 3.4 and I only wanted to hide some fields. I've used the export function only to find out how the "advanced" option works, because I couldn't find it in the documentation. I've added the "advanced" flags as you sugested and I've tested my model under 3.4 and it works. – Andrzej Nov 2 '19 at 10:01
  • Ah ok, yes indeed these advanced parameters work for QGIS 3.4. But your initial question is about exporting QGIS model as Python, which is very different. – etrimaille Nov 2 '19 at 10:06
1

This falls under the QgsProcessingParameterDefinition class where you can specify the flag for the advanced parameter:

QgsProcessingParameterDefinition.FlagAdvanced

You can check a number of scripts which use this method in the QGIS GitHub to see how it it used.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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