2

I am working on a plugin that would highlight selected features by changing its color. Can anyone help me regarding this? The plugin searches for required features and once found, I want that those features are highlighted with color different from the layer color. How to do this?

Here is the snippet:

def processResults(self, results):
    if self.layer is None:
        return
    if self.selectBox.isChecked():
        self.layer.setSelectedFeatures(results)
        if len(results) == 0:
            return

        if self.panBox.isEnabled() and self.panBox.isChecked():
            canvas = self.iface.mapCanvas()
            rect = canvas.mapRenderer().layerExtentToOutputExtent(self.layer, self.layer.boundingBoxOfSelected())
            if rect is not None:
                if self.scaleBox.isEnabled() and self.scaleBox.isChecked():
                    rect.scale(1.5)
                    canvas.setExtent(rect)
                else:
                    canvas.setExtent(QgsRectangle(rect.center(), rect.center()))
            canvas.refresh()

This code just scales the canvas 1.5 times, what I want is not zooming in it, but just highlight it on the same scale by either changing color or any other means. How to do it?

  • please help me anyone, its urgent – shahryar Oct 12 '14 at 12:01
  • 1
    no chance to look extensively - but this question and answer seems similar. – fluidmotion Oct 12 '14 at 14:37
  • Actually in the answer given in the above link does not tell how to highlight or color the feature differently from the rest of the layer. I have already used setSelectedFeature option (can be seen in the above edited code snippet) but it doesn't help. Can anyone help please? – shahryar Oct 13 '14 at 8:21
  • QGIS will automatically apply the selection color if the selection is set correctly. You should not have to do anything manually. – underdark Oct 13 '14 at 18:02
0

Difficult to tell what all your extra code is doing, but these two lines, executed in the Python console, select and highlight the first two features in the active layer:

l=iface.activeLayer()
l.setSelectedFeatures([1,2])
| improve this answer | |
  • thanks very much, finally I got my feature selected as well as highlighted. It was highlighted previously as well, but it was hidden another feature. – shahryar Oct 14 '14 at 10:14

Not the answer you're looking for? Browse other questions tagged or ask your own question.