4

I have a long script in PyQGIS that contains three processing tasks after another. I want to display a pop up box (like "QProgressDialog"-Box) for each task when it is finished.

Example: When Task 1 (Merge) has finished, I want a box to pop up ("Task 1 (Merge) is finished). Then the Task 2 is executing and when it is finished i want another box to pop up...

Unfortunately only the first "QProgressDialog"-Box is shown and QGIS freezes as it is processing.

How can I achieve to display a pop up box (or even better a progress bar) when QGIS is freezing?

My code is:

bar = QProgressDialog("Operation in progress", "Cancel",0,100)
bar.show()
bar.setRange(0,0)

#the processing tasks are following below
...
1
  • QGIS freezes as it is processing because you need to set 'setWindowModality' for QProgressDialog object and a sleep (delay) time for each task. Another issue is referred to 'setRange' method. It was already set in QProgressDialog object (as parameters ..., 0, 100). Please, see my answer.
    – xunilk
    Jan 1, 2021 at 18:18

2 Answers 2

2

QGIS freezes as it is processing because you need to set 'setWindowModality' for QProgressDialog object and a sleep (delay) time for each task. Another issue is referred to 'setRange' method. It was already set in QProgressDialog object (as parameters ..., 0, 100). So, for three processing tasks following code works as expected:

import time

m = ""

bar = QProgressDialog(m, "Cancel", 0, 100)

bar.setWindowModality(Qt.WindowModal)

m = "Operation 1 in progress"
bar.setLabelText(m)

for i in range(101):
    time.sleep(0.05)
    bar.setValue(i)

m = "Operation 2 in progress"
bar.setLabelText(m)

for i in range(101):
    time.sleep(0.05)
    bar.setValue(i)

m = "Operation 3 in progress"
bar.setLabelText(m)

for i in range(101):
    time.sleep(0.05)
    bar.setValue(i)

Following image is showing your "freezing behavior" when 'setWindowModality' line in above code is commented.

enter image description here

When 'setWindowModality' line is not commented, code behavior is as expected:

enter image description here

1

I don't think this can be achieved with dialog boxes. However, there is something better: the MessageBar. Have a look at: Is it possible to show our custom message in QGIS Status bar using python?

2
  • Thank you! I implemented the status bar in my plugin code but the message displays not before the whole process is finished.
    – POGO
    Dec 12, 2020 at 14:15
  • Sorry, I meant to refer to the MessageBar and the progress bar example in the above linked answer
    – underdark
    Dec 12, 2020 at 14:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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