Accessing the map canvas
Maps in QGIS are controlled through the map canvas. In this recipe, we'll access the canvas and then check one of its properties to ensure that we have control over the object.
Getting ready
The only thing you need to do for this recipe is to open QGIS and select Python Console from the Plugins menu.
How to do it...
We will assign the map canvas to a variable named canvas
. Then, we'll check the size
property of the canvas to get its size in pixels. To do this, perform the following steps:
- Enter the following line in the QGIS Python Console:
canvas = qgis.utils.iface.mapCanvas()
- Now, to ensure that we have properly accessed the canvas, check its size in pixels using the following line of code:
canvas.size()
- Verify that QGIS returns a
QSize
object that contains the canvas's pixel size as width and height respectively, similar to the following format. The actual width and height will vary based on the size of your QGIS window and screen size...