Graphics Reference
In-Depth Information
Working with selections
The vector layer class,
QgsVectorLayer
, includes support for keeping track of the
user's current selection. Doing this is relatively straightforward: there are methods that set
and alter the selection, as well as retrieve the selected features. When features are selected,
they are visually highlighted on the screen so that the user can see what has been selected.
Tip
If you create your own custom symbol layer, you will need to handle the highlighting of the
API
, in the section titled
Implementing symbol layers in Python
.
While there are several ways in which the user can select features, the most straightforward
way is to click on them. This can be implemented by using a simple map tool, for example:
class SelectTool(QgsMapToolIdentify):
def __init__(self, window):
QgsMapToolIdentify.__init__(self, window.mapCanvas)
self.window = window
self.setCursor(Qt.ArrowCursor)
def canvasReleaseEvent(self, event):
found_features = self.identify(event.x(), event.y(),
self.TopDownStopAtFirst,
self.VectorLayer)
if len(found_features) > 0:
layer = found_features[0].mLayer
feature = found_features[0].mFeature
if event.modifiers() & Qt.ShiftModifier:
layer.select(feature.id())
else:
layer.setSelectedFeatures([feature.id()])
else:
self.window.layer.removeSelection()