I want to obtain the color code of the current feature like this : Getting currentfeature's color in Expression Builder of QGIS?

But I am in QGIS 3.14 and I dont understand how to convert the script with the new API. I have changed

layer = QgsMapLayerRegistry.instance().mapLayersByName(layername)[0]
r = layer.rendererV2()


layer = QgsProject.instance().mapLayersByName(layername)[0]
r = layer.renderer()

But I have an error with

symbol = r.symbolForFeature(feature)
  • 1
    Try symbol = r.symbolForFeature(feature, QgsRenderContext()) Function signature with only feature (1 arg) disappeared (now always need two args) e.g qgis.org/api/… If OK, will made a formal answer. See also the breaking changes in the API between QGIS 2.x and 3.x. Your broken example is mentioned inside e.g qgis.org/api/… 2nd bullet point. This part of the doc could help you debug other issues while upgrading your PyQGIS code – ThomasG77 Jun 30 at 21:23

Your Answer

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

Browse other questions tagged or ask your own question.