1

I've got a QGIS processing script which displays progress in the message bar, using the code in the PyQGIS cookbook.

It displays properly at the beginning of processing, but my code calls some other processing algorithms from Grass, which also push messages to the message bar and override my progress bar. Is it possible to either set the default duration of these messages to something small like a second, or block the message entirely?

I tried an approach that connects the widgetAdded slot to a function to re-add the progress bar, but it's causing QGIS to crash. Here is the code:

def putBarBack():
    iface.messageBar().widgetAdded.disconnect()
    iface.messageBar().pushWidget(progressMessageBar, iface.messageBar().INFO)
    iface.messageBar().widgetAdded.connect(putBarBack)

def run():   
    #set up progress bar
    global progressMessageBar
    progressMessageBar = iface.messageBar().createMessage("Processing...")
    bar = QtGui.QProgressBar()
    bar.setRange(0,100)
    bar.setAlignment(Qt.AlignLeft|Qt.AlignVCenter)
    progressMessageBar.layout().addWidget(bar)
    iface.messageBar().pushWidget(progressMessageBar, iface.messageBar().INFO)

    iface.messageBar().widgetAdded.connect(putBarBack)

    # do some more processing here...

    bar.setValue(25)

    processing.runalg(grass7:r.watershed)

    bar.setValue(75)

    #do some more processing

    bar.setValue(100)
firefly-orange
  • 2,521
  • 6
  • 23
  • If you want to add messages to the progressMessageBar, you could just change its text. Example: at the beginning you have progressMessageBar = iface.messageBar().createMessage("Processing..."); then when running some process, use progressMessageBar.setText('Processing...Step 1'); then when running another process, use progressMessageBar.setText('Processing...Step 2') etc. That way, there will always be one progressMessageBar and all you're doing is changing the text (or message). – Joseph Jan 31 '18 at 15:26
  • Thanks @Joseph. The other algs I call are from the Grass library so I have no control over how they push messages to the bar. I've amended my code slightly to better explain my problem – firefly-orange Jan 31 '18 at 15:37
  • Ah, I think I understand. Have you tried silencing the progress messages? E.g. processing.runalg(grass7:r.watershed, progress=None) as described in this post. – Joseph Jan 31 '18 at 15:41
  • Yes, that works nicely. If you add your comment as an answer, I'll except. Thanks – firefly-orange Feb 07 '18 at 17:53
  • Glad it worked! I think this question should be marked as a duplicate of the one mentioned in my comment :) – Joseph Feb 08 '18 at 10:06

0 Answers0