3

In ArcGIS, when one clicks a feature with the identify tool, it will flash/flicker, which makes the feature really easy to see for a moment. Reportedly, there is also a flash method in its ArcObject API.

Is there a way to flash a set of selected feature to the same effect with PyQGIS (2.x or 3.0)?

I searched around, and couldn't find any mention of this or examples

7

We can build a flashFeatures method in this way (give it a try in the QGIS Python Console):

from qgis.gui import QgsHighlight
from PyQt4.QtCore import QTimer
from PyQt4.QtGui import QColor

timer = QTimer( iface.mapCanvas() )
lstHighlights = []

def flashFeatures( featureIds ):
    global lstHighlights
    for f in iface.activeLayer().getFeatures( QgsFeatureRequest().setFilterFids(featureIds) ):
        h = QgsHighlight( iface.mapCanvas(), f.geometry(), iface.activeLayer() ) 
        h.setColor( QColor( 255,0,0,255 ) )
        h.setWidth( 5 )
        h.setFillColor( QColor( 255,0,0,100 ) )
        lstHighlights.append( h )
    timer.start( 500 ) # Milliseconds before finishing the flash

def finishFlash():
    timer.stop()
    global lstHighlights
    lstHighlights = []

timer.timeout.connect( finishFlash )

Call flashFeatures method passing it selected features Ids (activate first your layer in the Layers panel):

flashFeatures( iface.activeLayer().selectedFeaturesIds() )

If you want to flash features each time they are selected, you could use:

iface.activeLayer().selectionChanged.connect( flashFeatures )

enter image description here

  • +1, I wonder if it will be nice to have the features flash x number of times consecutively but this is certainly great :) – Joseph Jun 29 '17 at 13:05
4

In 3.0 you can use QGIS's built in flashFeatureIds method on MapCanvas objects

canvas = iface.mapCanvas()
layer = iface.activeLayer()
my_features_ids = [42, 43, 45]
canvas.flashFeatureIds(layer, my_features_ids)

This method also lets you modify the style of the flashing

  • 1
    +1 Good to know. It's small typo, but in the last line should be 'my_featureS_ids'. – DIV Sep 19 at 14:07
  • Good point! Fixed – Jared Khan Sep 20 at 10:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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