I have been developing a python plugin that uses Processing algorithm for raster calculations. I want to support the gui with progress bar. How can i connect Processing job to progress bar? I found little description in here but no success.
Here is a way to set a primitive progress bar. Design is based on example you can found within the PyQgis Dev. Cookbook (see section 11.2)
Example is designed to work with features of a QgsVectorLayer but it shows the main steps you could adapt for your process algo.
The key to your problem is to find a way to evaluate the progression of your algo within itself.
1.Progress Bar is created outside of your processing function
2.Progress Bar is updated inside your processing function
I pass it to the iface.messageBar()
to make it easy
import time
#clear the message bar
qgis.utils.iface.messageBar().clearWidgets()
#set a new message bar
progressMessageBar = qgis.utils.iface.messageBar()
######################################
# Prepare your progress Bar
######################################
progress = QProgressBar()
#Maximum is set to 100, making it easy to work with percentage of completion
progress.setMaximum(100)
#pass the progress bar to the message Bar
progressMessageBar.pushWidget(progress)
#get a vector layer
vlayer = QgsMapLayerRegistry.instance().mapLayer("your layer")
vlayer.selectAll()
#Count all selected feature
count = vlayer.selectedFeatureCount()
#set a counter to reference the progress
i = 0
#start your processing loop
for feature in vlayer.selectedFeatures():
######################################
#Update the progress bar
######################################
i = i + 1
percent = (i/float(count)) * 100
progress.setValue(percent)
#optionnal, make your processing longer to enjoy your process bar :)
time.sleep(1)
######################################
#Do your processing stuff
######################################
qgis.utils.iface.messageBar().clearWidgets()
-
Peter - nicely commented and useful code - good framework for others to use, wish we had more examples like this in the documentation! @MSBilgin, did this work for you? – Simbamangu Sep 18 '14 at 5:27
If you would like to connect a Processing job to a progress bar, you can supply the progress bar using the progress parameter:
buff_res = processing.runalg("qgis:fixeddistancebuffer",
self.inpvl, 10, 10, True, None,
progress=myprogress)
See also: https://gis.stackexchange.com/a/237282