Bug report #10096
QGIS crashes when QgsRendererV2.symbolForFeature() is called from Python
|Affected QGIS version:||master||Regression?:||No|
|Operating System:||Easy fix?:||No|
|Pull Request or Patch supplied:||No||Resolution:||fixed/implemented|
|Crashes QGIS or corrupts data:||Yes||Copied to github as #:||18564|
With graduated styled layer, the following code crashes QGIS. It works in 2.2 without any problems.
layer = iface.activeLayer() renderer = layer.rendererV2() f = QgsFeature() layer.getFeatures().nextFeature(f) s = renderer.symbolForFeature(f)
With categorized styled layer, symbolForFeature() returns None. It returns a subclass object of QgsSymbolV2 correctly in 2.2.
Crash doesn't seem to occur if the graduated styled layer has been loaded through a project file, and a subclass object of QgsSymbolV2 is returned correctly, but with categorized styled layer, it still returns None.
This method is useful for plugins which export data with styles.
#1 Updated by Matthias Kuhn over 8 years ago
As a workaround for the crash: embed your calls within `renderer.startRender()` and `renderer.stopRender()` to have the renderer in a state where it is able to evaluate expressions.
In the long term it would however be nice to have somebody who knows this code to have a look at the lifetime of the expression which crashes.
#4 Updated by Alexander Bruy over 8 years ago
- Status changed from Closed to Reopened
Still true in latest master.Seems this method does not work as expected for almost all renderers:
- QgsSingleSymbolRendererV2 — returns feature symbol
- QgsCategorizedSymbolRendererV2 — returns None and in debug output I have message saying "there are no hashed symbols!!!"
- QgsGraduatedSymbolRendererV2 — just crashes QGIS
- QgsRuleBasedRendererV2 — returns None
Tested with point shapefile and same code as in initial post.
#5 Updated by Matthias Kuhn over 8 years ago
- Status changed from Reopened to Feedback
I think the workaround should be accepted as solution and stated in the API documentation according to 
The only thing left to fix is the crash in graduatedsymbolrenderer. The last "fix" was also just about the crash and not about changing the behavior to be compatible with calls out of startRender() / stopRender() context.