2

I have written a few functions to compare QGIS performance to MapInfo. Most of them involve doing some analysis then dumping the results on the map, as such the process would be considered 'finished' when the map has finished refreshing/rendering. I believe in this context refreshing/rendering are pretty much the same thing?

I've had a look around the documentation and have found references to (all under QgsMapCanvas class) renderStarting(), renderComplete(QPainter), mapCanvasRefreshed(). These are all 'signals', but I'm not sure how to use them (shame there are no examples in the documentation). Could anyone give me an example? Or is there a better approach? The important thing is to get an end-time when the map has finished rendering

2 Answers 2

3

Signal can be emited by an object. Ex. your canvas:

canvas = iface.mapCanvas()
custom_canvas = QgsMapCanvas() # if you want to create new canvas instead of this from main window

Canvas can emitt a signal renderComplete after rendering is complete, mapCanvasRefreshed after you refresh this canvas.

So you can connect this signal to the proper function and do something:

canvas.mapCanvasRefreshed.connect(custom_function)

Where the custom_function is simply:

def custom_function():
    # do something

Read more about signals and slots in the documentation: http://pyqt.sourceforge.net/Docs/PyQt4/new_style_signals_slots.html

Or in this tutorial: http://www.tutorialspoint.com/pyqt/pyqt_signals_and_slots.htm

0

I am using QGIS 3.10 and there i connect to the mapCanvas signals this way:

iface.mapCanvas().renderStarting.connect(self.start_time)
iface.mapCanvas().renderComplete.connect(self.stop_time)

This is how the class looks like. The 2 methods count the time and print a message:

import time

class MyPlugin:

    def __init__(self, iface):
        self.tic = None
        iface.mapCanvas().renderStarting.connect(self.start_time)
        iface.mapCanvas().renderComplete.connect(self.stop_time)

    def start_time(self):
        self.tic = time.perf_counter()
        print(f"Rendering started.")

    def stop_time(self):
        toc = time.perf_counter()
        print(f"Rendering finished in {toc - self.tic:0.4f} seconds.")

Now, simply open the Python Console inside Qgis and try to move the mapCanvas.

Your Answer

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

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