Sunday, 3 May 2015

Launching Processing tool User Interface using PyQGIS


I am trying to launch the UI of a processing algorithm using Python script but I cannot find a simple way for it (aside double clicking on it in the process toolbox of course). The plugin is a water network simulator and I just want to launch its pre-processing UI without using process.runalg() because this needs to enter arguments and I don't need it.


Some ways would be to trigger processes available in process toolbox or sending command to commander from python console. but I have no clue how and I am searching for 2 hours now, no results.



Answer



You can do that from the QGIS Python console in this way:


# Import both Processing and CommanderWindow 
# classes from the Processing framework.

from processing.core.Processing import Processing
from processing.gui.CommanderWindow import CommanderWindow

# Then get the algorithm you're interested in (for instance, Join Attributes):
alg = Processing.getAlgorithm("qgis:joinattributestable")

# Instantiate the commander window and open the algorithm's interface
cw = CommanderWindow(iface.mainWindow(), iface.mapCanvas())
if alg is not None:
cw.runAlgorithm(alg)


That's it! You get the algorithm's UI open.


enter image description here


Note: You can get the names of the available algorithms by entering these lines in the QGIS Python console:


import processing
processing.alglist()

Tested in QGIS 2.10.1, Processing framework v.2.9.1 and v.2.10.1




EDIT:



If you find errors when running the code snippet above, chances are you're using QGIS v2.16.0, v2.16.1, or v2.16.2. To solve the issue, install QGIS v2.16.3.


No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...