2

the following code enables to select and highlight the feature with the id 5, further, the highlight is shown in red:

from qgis.core import *

iface.mapCanvas().setSelectionColor( QColor("red") ) l=iface.activeLayer() l.selectByIds([5])

If all worked as expected, you will see the feature with id 5 highlighted in red in your map view, like so: feature selected with pyqgis with custom color (red)

However, as one can see, the highlighting is much thinner than the QGIS default highlight.

How can I adjust the width of the hightlight with Pyqgis? I think at some code line as iface.mapCanvas().setSelectionWidth( QWidth(0.4) )

i.i.k.
  • 1,427
  • 5
  • 11

1 Answers1

4

The selection highlighting inherits the width from the line style, it does not have its own value. You can however create your own selection highlight, which you can style to your choosing:

p = QgsProject.instance()
lyr = iface.activeLayer()

get the id of the layer to check if it has been removed later

lyr_id = lyr.id()

initialise a list to store the current highlights

selection_highlights = []

def highlightSelection(): # give the highlight list global scope global selection_highlights

# clear all existing highlights (created by this function) from the canvas
for highlight in selection_highlights:
    iface.mapCanvas().scene().removeItem(highlight)    

# empty the highlights list now that none exist in the canvas
selection_highlights = []

if lyr_id in p.mapLayers().keys():

    # get a list of selected features
    selected = lyr.selectedFeatures()

    # iterate through features and create a highlight for each
    for feat in selected:
        h = QgsHighlight(iface.mapCanvas(), feat.geometry(), lyr)
        h.setWidth(3)
        h.setColor(QColor('red'))
        # store each highlight in the list so it can easily be removed
        selection_highlights.append(h)

def removeHighlights(): # if the layer has been removed, clear all the highlights from the canvas if lyr_id not in p.mapLayers().keys(): global selection_highlights for highlight in selection_highlights: iface.mapCanvas().scene().removeItem(highlight) selection_highlights = []

connect the functions to the relevant signals

conn1 = lyr.selectionChanged.connect(highlightSelection) conn2 = QgsProject.instance().layersRemoved.connect(removeHighlights)

to disconnect

#lyr.selectionChanged.disconnect(conn1) #QgsProject.instance().layersRemoved.disconnect(conn2)

enter image description here

Matt
  • 16,843
  • 3
  • 21
  • 52
  • Thank you very much! This even helps with another issue I had to solve, namely to select two fetaures on top of each other. – i.i.k. Mar 31 '23 at 07:49
  • The highlighting now looks thicker, however, I now have the problem that selected line features are frozen in the map view, even after I removed all layer from the map. How can I remove them? I did see the disconnecting code line, but am not sure how to properly use them. I just uncommented the line, in hope this would reset the selection, however, it doesn't do it. Could you elaborate a bit on that line to disconnect? – i.i.k. Mar 31 '23 at 08:21
  • You can use the disconnect when you don't want the behaviour to be exhibited on the layer anymore. I just added another function that connects to the layersRemoved signal of the QgsProject.instance(). This means that the highlights will be cleared if the highlighted layer is removed from the project. – Matt Mar 31 '23 at 09:45