Since I sometimes have to process cross product of two vector layers with geoprocessing scripts, causing me to use nested loops like

source_count = source_layer.featureCount()
target_count = target_layer.featureCount()
nsource = 1.0
for f_source in source_layer.getFeatures():
    ntarget = 1.0
    for f_target in target_layer.getFeatures():
        #do something
        targetprogress = 100 * (ntarget / target_count)
        ntarget += 1
    sourceprogress = 100  *(nsource / source_count)
    nsource += 1

When both layers consist of, let's say, some ten thousands of features, processing can be extremely time consuming, and to communicate the user that the processing is still alive, it would make sense to have a progress bar for each inner and outer loop.

I don't think this can be achieved with standard processing gui tools, but this combined with some Qt widgets? I'd like to do the processing in a script, not a plugin, in wich's Qt form various QprogressBars would be easy to implement.

  • 1
    Can't you import what is necessary into your script? from PyQt4.QtGui import QProgressBar from qgis.gui import QgsMessageBar or something like this and then use it? iface.messageBar().pushWidget(QProgressBar(), QgsMessageBar().INFO, 0) – Victor Sep 28 '16 at 10:33
  • Quite helpful workaround. So now I have two progress bars, one in the scripts automatically generated gui and one in the message bar. Gives nice and entertaining screen animations for my nested loops ;-) @Victor consider to add this as an answer! – Jochen Schwarze Sep 28 '16 at 10:57
  • But beware! Updating a progress bar in the inner loop is extremely time consuming and slows down processing significantly! Perhaps two progress bars in such cases doesn't belong to the top 100 ideas I ever had... @Victor: possibly add this warning to you answer – Jochen Schwarze Sep 28 '16 at 11:03

You could import the necessary functions into your script with:

from PyQt4.QtGui import QProgressBar
from qgis.gui import QgsMessageBar

Then you can call the progress bar with:

msgBar= iface.messageBar()
pb= QProgressBar()
msgBar.pushWidget(pb, QgsMessageBar().INFO, 0)

and update the value in the progress bar in your loop:


However, as you pointed out, updating a progress bar in your inner loop can slow down the process. Cheers,

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.