In QGIS 3.4, I would like to automatize and uniformize the appearance of each layer.
I would like that each new layer is created:
- with the alpha slider activated
- with Feature count activated
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It only takes a minute to sign up.
Sign up to join this communityYou could use something like the following in the Python Console which:
These are applied whenever a layer is added:
def enableFeatCountAndAlphaSlider(layers):
root = QgsProject.instance().layerTreeRoot()
layer = layers[0]
# Enable feature count for vector-type layers
if layer.type() == QgsMapLayer.VectorLayer:
myLayerNode = root.findLayer(layer.id())
myLayerNode.setCustomProperty("showFeatureCount", True)
# Enable transparency slider
if layer.customProperty("embeddedWidgets/count") != 1 or layer.customProperty("embeddedWidgets/0/id") != u'transparency':
layer.setCustomProperty("embeddedWidgets/count", 1)
layer.setCustomProperty("embeddedWidgets/0/id", "transparency")
# Refresh legend symbology
iface.layerTreeView().refreshLayerSymbology(layer.id())
# Connect "legendLayersAdded" event to "enableFeatCountAndAlphaSlider" function
QgsProject.instance().legendLayersAdded.connect(enableFeatCountAndAlphaSlider)
To disconnect the function, use:
QgsProject.instance().legendLayersAdded.disconnect(enableFeatCountAndAlphaSlider)
QgsProject.instance().legendLayersAdded.connect(enableFeatCountAndAlphaSlider)
from qgis.core import QgsMapLayer, QgsProject
and from qgis.utils import iface
at the beginning. After QGIS has loaded, it works. Did you put it in the same directory called "Python" or did you put it inside this directory? I'm not a linux user so not sure if this is the correct path or not. If it is correct then perhaps the issue lies elsewhere.