TransWikia.com

QGIS crashes when setting new Symbol to layer with python

Geographic Information Systems Asked by Blinxen on January 16, 2021

I followed the QGIS 3 cookbook and created a new custom symbol layer class. But when I try to create a new the symbol, QGIS crashes. I create it like this.

symbol = QgsMarkerSymbol({FooMarker(20)})

The link for the cookbook is the following: https://docs.qgis.org/testing/pdf/en/QGIS-testing-PyQGISDeveloperCookbook-en.pdf page: 31

My FooMarker class looks like this.

class FooMarker(QgsMarkerSymbolLayer):
    def __init__(self, radius=4.0):
        QgsMarkerSymbolLayer.__init__(self)
        self.radius = radius
        self.color = QColor(255,0,0)

    def layerType(self):
        return "FooMarker"

    def properties(self):
        return {"radius": str(self.radius)}

    def startRender(self, context):
        pass

    def stopRender(self, context):
        pass

   def renderPoint(self, point, context):
        color = context.selectionColor() if context.selected() else self.color
        p = context.renderContext().painter()
        p.setPen(color)
        p.drawEllipse(point, self.radius, self.radius)

    def clone(self):
        return FooMarker(self.radius)

One Answer

According to QGIS API Documentation QgsMarkerSymbol class needs QgsSymbolLayerList. That means a list in Python.

QgsMarkerSymbol (const QgsSymbolLayerList & layers = QgsSymbolLayerList())

But you pass a dictionary to QgsMarkerSymbol constructor method. If you change {FooMarker(20)} into [FooMarker(20)], QGIS doesn't crash.

Correct answer by Kadir Şahbaz on January 16, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP