Graphics Reference
In-Depth Information
The Get Info map tool
When the user clicks on the Get Info item in the toolbar, we will activate a custom map
tool that lets the user click on a track to display and edit the attributes for that track. Let's
walk through this implementation one step at a time, starting with the GetInfoTool
class itself. Add the following to your mapTools.py module:
class GetInfoTool(QgsMapTool, MapToolMixin):
def __init__(self, canvas, layer, onGetInfo):
QgsMapTool.__init__(self, canvas)
self.onGetInfo = onGetInfo
self.setLayer(layer)
self.setCursor(Qt.WhatsThisCursor)
def canvasReleaseEvent(self, event):
if event.button() != Qt.LeftButton: return
feature = self.findFeatureAt(event.pos())
if feature != None:
self.onGetInfo(feature)
This map tool calls the onGetInfo() method (which is passed as a parameter to the map
tool's initializer) when the user clicks on a track. Let's now use this map tool within our
program by adding the following code to the end of our setupMapTools() method in
the forestTrails.py module:
self.getInfoTool = GetInfoTool(self.mapCanvas,
self.trackLayer,
self.onGetInfo)
self.getInfoTool.setAction(self.actionGetInfo)
We can then replace our placeholder getInfo() method with the following:
def getInfo(self):
self.mapCanvas.setMapTool(self.getInfoTool)
This activates the map tool when the user clicks on the toolbar icon. The last step is to im-
plement the onGetInfo() method, which is called when the user selects the map tool
and clicks on a track.
Search WWH ::




Custom Search