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.
The only thing you need to do for this recipe is to open QGIS and select Python Console from the Plugins menu.
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:
canvas = qgis.utils.iface.mapCanvas()
canvas.size()
QSize
object that contains the canvas's pixel size, similar to the following format:PyQt4.QtCore.QSize(698, 138)
Everything in QGIS centers on the canvas. The canvas is part of the QGIS interface or iface API. Anything you see on the screen when using QGIS is generated through the iface API. Note that the iface
object is only available to scripts and plugins. When you are building a standalone application, you must initialize your own QgsMapCanvas
object.