3

I am upgrading some QGIS 2 plugins to QGIS 3 and need to do some debugging. It appears that the canvas does not update until the code stops running, regardless of any calls to self.iface.mapCanvas().refresh().

Is there a way to update the QGIS canvas while stepping through code in PyCharm? I am using QGIS 3.6 and PyCharm 2019.1

  • Not sure how that might work, but I could see this being a really neat feature within a Jupyter Notebook. – Trevor J. Smith May 13 '19 at 17:51
  • Is it possible that the Qt event loop isn't doing what it normally does? Does the map canvas handle interactions like drag/zoom etc or is it completely non-responsive? – Spacedman May 13 '19 at 19:07
  • @Spacedman, it's non-responsive. Blue circle of death. – CNIDog May 13 '19 at 21:23
  • 1
    Then it seems that at the debug prompt the event loop isn't happening. I vaguely recall there's a Qt way of saying "go handle any pending events now".... Try the processEvents method on the QApplication object.. doc.qt.io/archives/qt-4.8/qcoreapplication.html#processEvents – Spacedman May 14 '19 at 6:56
  • 1
    Yes, @Spacedman, that was the correct answer that would have saved me (and will save me in the future) many hours of frustrating debugging! Thanks. – CNIDog May 15 '19 at 13:39
2

While your debugger is waiting for you to inspect objects or step to the next line, Qt can't process any pending events, so event-driven parts of the application will become unresponsive.

There's a method on the QApplication object that forces it to deal with everything on the event queue and then return: See: http://doc.qt.io/archives/qt-4.8/qcoreapplication.html#processEvents for documentation. So calling app.processEvents() should update the map canvas.

I'm not sure what the QApplication object (app above) will be in your debugging session - if you can add this in a comment or edit that will be great.

  • 1
    In my case, simply running QApplication.processEvents() was sufficient. I can even run this from the Eval window in PyCharm so as not to leave the processEvents() lines strewn about the code, including the requisite from PyQt5 import QApplication in the Evaluation window if it is not otherwise required and already in the declarations. – CNIDog May 15 '19 at 16:41
  • 1
    Right - I wasn't sure if QApplication was a module with a constructor for creating QApplication objects or was, as it seems, an instance object instead. If that makes sense... – Spacedman May 15 '19 at 21:18

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.