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?
2 Answers 2
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!user89163– user891632017年05月03日 17:28:01 +00:00Commented May 3, 2017 at 17:28
-
1Thanks for your response you helped me figure out the answer to my question.user89163– user891632017年05月03日 17:42:11 +00:00Commented 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).mgri– mgri2017年05月03日 18:56:04 +00:00Commented May 3, 2017 at 18:56
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.
Explore related questions
See similar questions with these tags.