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?

mgri
16.4k6 gold badges48 silver badges80 bronze badges
asked May 3, 2017 at 16:09

2 Answers 2

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).
answered May 3, 2017 at 17:04
3
  • 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! Commented May 3, 2017 at 17:28
  • 1
    Thanks for your response you helped me figure out the answer to my question. Commented May 3, 2017 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). Commented May 3, 2017 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.

answered May 3, 2017 at 17:41

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.