Exploring iface and QGis
The iface
class used in the preceding snippets is important in every PyQGIS code; it is used to access most graphical QGIS components, from displayed layers to the toolbar buttons.
Note
The iface
class is a Python wrapper for the C++ class, QgisInterface
, which is documented at http://qgis.org/api/classQgisInterface.html. Most QGIS classes have a Qgs
prefix. Some special classes can have the Qgis
or QGis
prefixes.
The prefix Qgs
is the Qt
namespace registered by Gary Sherman, the QGIS creator, so Qt (Q) and Gary Sherman (gs).
The most common use of the iface
class is to get a reference of the canvas
where maps are displayed:
canvas = iface.mapCanvas()
The class can also be used as a shortcut to load raster or vector layers; for example loading the raster, path/to/my/raster.tif
, and naming it myraster
in the legend panel. This can be done by typing the following command:
iface.addRasterLayer("path/to/my/raster.tif", "myraster")
Note
Pay attention to writing paths with Windows...