Graphics Reference
In-Depth Information
canvas.useImageToRender(False)
canvas.setCanvasColor(Qt.white)
canvas.show()
layer = QgsVectorLayer(shapefile, "layer1", "ogr")
if not layer.isValid():
raise IOError("Invalid shapefile")
QgsMapLayerRegistry.instance().addMapLayer(layer)
canvas.setExtent(layer.extent())
canvas.setLayerSet([QgsMapCanvasLayer(layer)])
layout = QVBoxLayout()
layout.addWidget(canvas)
contents = QWidget()
contents.setLayout(layout)
self.setCentralWidget(contents)
Don't worry too much about the details of this class; we basically just create a window
and place a QgsMapCanvas object within it. We then create a map layer (an instance of
QgsVectorLayer ) and add it to the map canvas. Finally, we add the canvas to the win-
dow's contents.
Notice that QgsMapCanvas and QgsVectorLayer are both part of PyQGIS, while
QMainWindow , QVBoxLayout , and QWidget are all PyQt classes. This application
uses the PyQGIS classes within a PyQt application, mixing the classes from both sources.
This is possible because QGIS is built using Qt, and the various PyQGIS classes are based
on PyQt.
To turn the preceding code into a working application, all we need to do is add some more
import statements to the top of the module:
import sys
from PyQt4.QtGui import *
from PyQt4.QtCore import Qt
Search WWH ::




Custom Search