Graphics Reference
In-Depth Information
QgsApplication.exitQgis()
Of course, this application doesn't do anything useful yet—it simply starts up and shuts
down the PyQGIS libraries. So let's replace the " ... " line with some useful code that dis-
plays a basic map widget. To do this, we need to define a QMainWindow subclass, which
displays the map widget, and then create and use a QApplication object to display this
window and handle the various user-interface events while the application is running.
Note
Both QMainWindow and QApplication are PyQt classes. We will be working ex-
tensively with the various PyQt classes as we develop our own external applications using
QGIS and Python.
Let's start by replacing the " ... " line with the following code, which displays a map
viewer and then runs the application's main event loop:
app = QApplication(sys.argv)
viewer = MapViewer("/path/to/shapefile.shp")
viewer.show()
app.exec_()
As you can see, a MapViewer instance (which we will define shortly) is created and dis-
played, and the QApplication object is run by calling the exec_() method. For sim-
plicity, we pass the name of a shapefile to display within the map viewer.
Running this code will cause the map viewer to be displayed, and the application will run
until the user closes the window or chooses the Quit command from the menu.
Now, let's define the MapViewer class. Here is what the class definition looks like:
class MapViewer(QMainWindow):
def __init__(self, shapefile):
QMainWindow.__init__(self)
self.setWindowTitle("Map Viewer")
canvas = QgsMapCanvas()
Search WWH ::




Custom Search