0

I am working on a script (plugin) that allows user to :

  • select a feature by attributes,

  • zoom to that feature,

  • Create a map in layout designer focused on that feature,

My problem is that the feature is not highlighted in the map layout.

I followed this topic [using QgsHighlight Class] but the result is still the same. The feature is not highlighted.

I tried the rubberband Option. Same result as QgsHighlight Class.

Note : I am using QGIS 3.18.2

1 Answer 1

4

General advice: try via the QGIS GUI to achieve what you want then see how to implement it in PyQGIS. It's what I've done here.

You do not need to use any code in particular, just using an expression for fill color style is enough.

Without caring about using PyQGIS first, just try manually the following:

  • On your layer you want to select, just set the fill color expression with
if(
  is_selected(),
  color_rgb(255,0,0),
  color_rgb(0,0,255)
)
  • Select features graphically using QGIS GUI
  • Switch to your layout (I suppose it contains a map element that display your layer)
  • Refresh your layout to see style has changed for selected feature(s)

At PyQGIS level, to change style in expression, you should use a code like below

layer = iface.activeLayer()

# tested for single symbol, single marker here not for more complex styles
propertyColor = QgsProperty()
propertyColor.setExpressionString("if(is_selected(), color_rgb(255,0,0), color_rgb(0,0,255))")
propertyColor.setActive(True)
layer.renderer().symbol().symbolLayer(0).dataDefinedProperties().setProperty(QgsSymbolLayer.PropertyFillColor, propertyColor)
layer.triggerRepaint()
6
  • the first option worked fine. the second returns the following error : AttributeError: 'QgsRuleBasedRenderer' object has no attribute 'symbol'
    – Rid El Mou
    Commented Apr 28, 2021 at 19:21
  • My code is not for QgsRuleBasedRenderer but QgsSingleSymbolRenderer e.g my comment "tested for single symbol, single marker here not for more complex styles"
    – ThomasG77
    Commented Apr 28, 2021 at 19:31
  • 1
    You can change line layer.renderer().symbol().symbolLayer(0).dataDefinedProperties().setProperty(QgsSymbolLayer.PropertyFillColor, propertyColor) with layer.renderer().rootRule().symbols()[0].symbolLayer(0).dataDefinedProperties().setProperty(QgsSymbolLayer.PropertyFillColor, propertyColor) but as I do not know the exact styles in your QgsRuleBasedRenderer I can't help more
    – ThomasG77
    Commented Apr 28, 2021 at 19:37
  • The last line worked fine. thx again @ThomasG77
    – Rid El Mou
    Commented Apr 28, 2021 at 21:49
  • What you call the 2nd option is exactly doing the same as the 1st just with PyQGIS. First approach is more about methodology to sort things through GUI whenever possible before automating.
    – ThomasG77
    Commented Apr 28, 2021 at 22:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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