4

I have a stand-alone pyqgis application that can display vector data. I have implemented a routine that sets layer's renderer as below:

def set_sym(layer):
    symbol = QgsMarkerSymbolV2.createSimple({})
    symbol_layer = QgsSimpleMarkerSymbolLayerV2()
    symbol_layer.setColor(QColor(0, 0, 0, 255))
    symbol_layer.setName('circle')
    symbol_layer.setSize(1.0)
    symbol.appendSymbolLayer(symbol_layer)
    renderer = QgsSingleSymbolRendererV2(symbol)
    layer.setRendererV2(renderer)

It works. However, if I call it a second time on a layer, then immediately go to exit the program, it hangs & I have to kill the background python process to get out of it. I've already checked that the newly created renderer and the layer.rendererV2() are different and their symbol() are also different (I mean the memory addresses are different). So, it makes me think that the setRendererV2() routine is not replacing the renderer cleanly, hence hanging the program upon quitting right after the second call on the same layer. Please let me know if there is something I missed that caused this.

  • @GermánCarrillo, tried the clone() call, still hang. – For Comment Jul 8 '17 at 3:58
  • @GermánCarrillo, I think I understand your logic, that is to ensure a new renderer instance is used to replace the old one. I thought the renderer = QgsSingleSymbolRendererV2(symbol) call achieves that, right? – For Comment Jul 8 '17 at 4:09
2

Maybe there are some problems with:

symbol = QgsMarkerSymbolV2.createSimple({})

I ran this code many times and QGIS didn't crash:

from PyQt4.QtGui import QColor

layer = iface.activeLayer() # load the layer as you want
symbol = QgsMarkerSymbolV2.defaultSymbol(layer.geometryType())
symbol_layer = QgsSimpleMarkerSymbolLayerV2()
symbol_layer.setColor(QColor(0, 0, 0, 255))
symbol_layer.setName('circle')
symbol_layer.setSize(1.0)
symbol.appendSymbolLayer(symbol_layer)
renderer = QgsSingleSymbolRendererV2(symbol)
layer.setRendererV2(renderer)
layer.triggerRepaint()
  • tried your suggestion, didn't solve the problem, it still hangs the program. Also, my application is stand-alone pyqgis application, not a script or plugin within the full QGIS program. – For Comment Jul 8 '17 at 4:05
2

The solution is to deselect the active layer that is passed into the routine right after the symbology change. This will break whatever lingering references upon exiting the program.

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.