[Qgis-user] Event loop in QGIS3

chris hermansen clhermansen at gmail.com
Tue Aug 16 13:39:33 PDT 2022


Marian and list,

On Tue, Aug 16, 2022 at 12:02 PM Tudorache, Marian via Qgis-user <
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

There you will see this:

The event loop is started with a call to exec
> <https://doc.qt.io/qt-5/qcoreapplication.html#exec>(). Long-running
> operations can call processEvents
> <https://doc.qt.io/qt-5/qcoreapplication.html#processEvents>() 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.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/qgis-user/attachments/20220816/ec8d3b28/attachment.htm>


More information about the Qgis-user mailing list