4

Steps to reproduce:

Create a new project->Add new Shapefile Layer(Polygon)->Click on the newly added Layer->Toggle Editing->Using the 'Add Feature' Tool draw a polygon and save the layer

Open the python console->Run the following lines of code in the python console

from PyQt4.QtGUI import QColor

layer = iface.activeLayer()
symbol = QgsLineSymbolV2.createSimple({})
symbol.deleteSymbolLayer(0)
symbol_layer = QgsSimpleLineSymbolLayerV2(QColor('red'), 2.0)
symbol.appendSymbolLayer(symbol_layer.clone())
layer.rendererV2().setSymbol(symbol.clone())
layer.triggerRepaint()
iface.legendInterface().refreshLayerSymbology(layer)

After running the above code the polygon stops displaying. Is this a bug in QGIS 2.18 or am I missing something?

4

I think you can't successfully apply a QgsSimpleLineSymbolLayerV2() symbol to a polygon because it belongs to linestring geometries.

The allowed symbols for polygons are:

  • Simple fill (QgsSimpleFillSymbolLayerV2);
  • Gradient fill (QgsFillSymbolLayerV2);
  • Centroid fill (QgsCentroidFillSymbolLayerV2);
  • Line pattern fill (QgsLinePatternFillSymbolLayer);
  • Point pattern fill (QgsPointPatternFillSymbolLayer);
  • SVG fill (QgsSVGFillSymbolLayer);
  • Outline: marker line (QgsMarkerLineSymbolLayerV2);
  • Outline: simple line (QgsSimpleLineSymbolLayerV2).
  • According to your response QgsSimpleLineSymbolLayerV2 can not be successfully applied to a polygon. If this is the case why are including it in your list of allowed symbols for polygons? Your answer is confusing! – Software Guy May 3 '17 at 17:28
  • 1
    Thanks for your response you helped me figure out the answer to my question. – Software Guy May 3 '17 at 17:42
  • @SoftwareGuy you are welcome. If you want to perform any specific operation with symbol layer I will try to help you (but you should ask a new separate question). – mgri May 3 '17 at 18:56
2

The problem with my code above is the symbol I was using. Once I changed the symbol to use the QgsLinePatternFillSymbolLayer object polygon displayed on the layer as expected.

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.