[Qgis-user] Event loop in QGIS3

Tudorache, Marian Marian.Tudorache at navcanada.ca
Tue Aug 16 13:53:54 PDT 2022


Hi Chris,

I read that article and based on it I found out that I need to put processEvents() and I understand the logic in this.

Someone said:
In more recent QGIS versions the Python console no longer triggers this event loop, as it's often quite dangerous to execute the loop in the middle of other running code.

It seems somehow QGIS 2.18 was designed to send automatically updates to the GUIs when a loop is running and this capability was eliminated in QGIS 3
Anyway, thank you for your answer.

Marian

From: chris hermansen <clhermansen at gmail.com>
Sent: August 16, 2022 4:40 PM
To: Tudorache, Marian <Marian.Tudorache at navcanada.ca>
Cc: qgis-user at lists.osgeo.org
Subject: [EXT] Re: [Qgis-user] Event loop in QGIS3

Marian and list, On Tue, Aug 16, 2022 at 12: 02 PM Tudorache, Marian via Qgis-user <qgis-user@ lists. osgeo. org> wrote: Hello community,   A while ago I sent a question about a problem with QMessageBox and message bar does not get updated

Marian and list,

On Tue, Aug 16, 2022 at 12:02 PM Tudorache, Marian via Qgis-user <qgis-user at lists.osgeo.org<mailto:qgis-user at lists.osgeo.org>> wrote:
Hello community,

A while ago I sent a question about a problem with QMessageBox and message bar does not get updated during a long process.
In my example I create a script like this.

from qgis.PyQt import QtWidgets
message = "Wait to open the airspace project..."
msg = QtWidgets.QMessageBox()
msg.setText(message)
msg.show()
qgis.utils.iface.messageBar().pushMessage("Wait", message)


for i in range(1000):
    print(i)
qgis.utils.iface.messageBar().pushMessage("Done", "The process is done")
msg.setText("The process is done")
msg.show()
qgis.utils.iface.messageBar().pushMessage("Done", "The process is done"))


When I run the scrip in QGIS 2.18 the QMessageBox displays first the message "Wait to open the airspace project...". The same message is pushed to messageBar.
Then during the loop it displays the variable i.
At the end of the loop the QMessageBox displays the message "The process is done" which is also pushed to the messageBar.

When I run the same scrip on QGIS 3 the QMessageBox is empty. The message "Wait to open the airspace project..." is not displayed by QMessageBox nor by messageBar.
The print inside the loop displays the I and when the loop is done the QMessageBox displays the message "The process is done".
The messageBar displays also the message "The process is done". And after a while it displays the initial message "Wait to open the airspace project..."

I modified the scrip by adding QApplication.processEvents() inside the loop and now everything is fine.
It seems QGIS3 no longer triggers this event loop and I have to do this explicitly by calling processEvents from QApplication.

Why QGIS 3 needs to call QApplication.processEvents() and QGIS 2 does not need?


Marian, did you read this article?

https://doc.qt.io/qt-5/qcoreapplication.html#details<https://urldefense.com/v3/__https:/doc.qt.io/qt-5/qcoreapplication.html*details__;Iw!!P0kg3YazkQ!CcSBA7taBRD4nM94gtxzazyv0U6cmASgYsOj82Fb8dTTzEuCUBZIOlqx018StM8IglMsIkz51FTlYFb0J6ClVfIwQbFuLA$>

There you will see this:

The event loop is started with a call to exec<https://urldefense.com/v3/__https:/doc.qt.io/qt-5/qcoreapplication.html*exec__;Iw!!P0kg3YazkQ!CcSBA7taBRD4nM94gtxzazyv0U6cmASgYsOj82Fb8dTTzEuCUBZIOlqx018StM8IglMsIkz51FTlYFb0J6ClVfLZXx-a2Q$>(). Long-running operations can call processEvents<https://urldefense.com/v3/__https:/doc.qt.io/qt-5/qcoreapplication.html*processEvents__;Iw!!P0kg3YazkQ!CcSBA7taBRD4nM94gtxzazyv0U6cmASgYsOj82Fb8dTTzEuCUBZIOlqx018StM8IglMsIkz51FTlYFb0J6ClVfI1OxPavQ$>() to keep the application responsive.

In your case, your "long-running operation" is your for loop.  And it is not QGIS3 that has the responsibility to trigger processEvents() but your own long-running operation, at least the way I'm reading this.

As to why QGIS 2 did not need this, I don't know.

--
Chris Hermansen · clhermansen "at" gmail "dot" com

C'est ma façon de parler.

________________________________

This electronic message, as well as any transmitted files included in the electronic message, may contain privileged or confidential information and is intended solely for the use of the individual(s) or entity to which it is addressed. If you have received this electronic message in error please notify the sender immediately and delete the electronic message. Any unauthorized copying, disclosure or distribution of the electronic message is strictly forbidden. NAV CANADA accepts no liability for any damage caused by any virus and/or other malicious code transmitted by this electronic communication.

Le présent message électronique et tout fichier qui peut y être joint peuvent contenir des renseignements privilégiés ou confidentiels destinés à l’usage exclusif des personnes ou des organismes à qui ils s’adressent. Si vous avez reçu ce message électronique par erreur, veuillez en informer l’expéditeur immédiatement et supprimez le. Toute reproduction, divulgation ou distribution du présent message électronique est strictement interdite. NAV CANADA n’assume aucune responsabilité en cas de dommage causé par tout virus ou autre programme malveillant transmis par ce message électronique.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/qgis-user/attachments/20220816/07c96918/attachment.htm>


More information about the Qgis-user mailing list