1

I am updating a QGIS2 plugin to QGIS3. The following code in PyQGIS2 works:

        association_symbol = QgsSymbolLayerV2Registry.instance().symbolLayerMetadata("ArrowLine").createSymbolLayer({
        'arrow_width': '0.4',
        'head_length': '2.0',
        'head_thickness' : '0.75',
        'color': 'red',
        'line_color': 'red',
        'is_curved': '0',
        'arrow_start_width': '.2'
    })
    self.road_object_path_association_layer.rendererV2().symbols()[0].appendSymbolLayer(association_symbol)

I made an attempt at correcting for PyQGIS3, but it does not work:

        association_symbol = QgsApplication.symbolLayerRegistry().symbolLayerMetadata("ArrowLine").createSymbolLayer({
        'arrow_width': '0.4',
        'head_length': '2.0',
        'head_thickness' : '0.75',
        'color': 'red',
        'line_color': 'red',
        'is_curved': '0',
        'arrow_start_width': '.2'
    })
    self.road_object_path_association_layer.renderer().symbol().symbolLayers()[0].appendSymbolLayer(association_symbol)

The error returned is as follows:

AttributeError: 'QgsSimpleLineSymbolLayer' object has no attribute 'appendSymbolLayer'

The symbols() object required an argument. I was not sure what argument to provide. Further research let me to believe I could get the info from the symbolLayers()[0] object, but that appears to have been a red herring. Any assistance would be helpful.

1 Answer 1

3

You don't need symbolLayers()[0] this,

road_object_path_association_layer.renderer().symbol()

is a QgsLineSymbol that inherits from QgsSymbol and here you find the method appendSymbolLayer

Code:

association_symbol = QgsApplication.symbolLayerRegistry().symbolLayerMetadata("ArrowLine").createSymbolLayer({
        'arrow_width': '0.4',
        'head_length': '2.0',
        'head_thickness' : '0.75',
        'color': 'red',
        'line_color': 'red',
        'is_curved': '0',
        'arrow_start_width': '.2'
    })
road_object_path_association_layer.renderer().symbol().appendSymbolLayer(association_symbol)
road_object_path_association_layer.triggerRepaint() # Add repaint layer

Class Reference

2
  • Thanks, @Fran, for your assistance and your patience with a noob!
    – CNIDog
    Commented May 22, 2019 at 18:19
  • 1
    Your weolcome. Accept it as an answer if it solved your problem
    – Fran Raga
    Commented May 22, 2019 at 18:24

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.