[GIS] Setting vector layer custom properties using PyQGIS

labelingpyqgis

I'm trying to set some labels and styles for some vector layers that are loaded programatically from a Spatialite database. I have the following code, which returns no error, and includes the changes in the .qgis file, but does not show on the canvas:

uri = QgsDataSourceURI()  
uri.setDatabase(database_path)
uri.setDataSource('', table_name, 'geometry')
layer = QgsVectorLayer(uri.uri(), display_name, 'spatialite')
QgsMapLayerRegistry.instance().addMapLayer(layer)

layer.setCustomProperty("labeling/enabled", "true")  
layer.setCustomProperty("labeling/fontFamily", "Arial")
layer.setCustomProperty("labeling/fontSize", "10")
layer.setCustomProperty("labeling/fieldName", "source_id")
layer.setCustomProperty("labeling/placement", "2")

How can I get this to work?

Best Answer

You have to set the custom property that enables labeling in the first place.

layer.setCustomProperty("labeling", "pal")
layer.setCustomProperty("labeling/enabled", True)
Related Question