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
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:
You 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)
C:\Users\You\AppData\Roaming\QGIS\QGIS3\profiles\default\python\startup.py? When you and you run QGIS, you might receive errors such asmodule x is not definedetc. It just means you need to import these at the beginning of the script (i.e. import module x). – Joseph Aug 01 '19 at 14:17/$HOME/.local/share/QGIS/QGIS3/profiles/default/pythonI do not get any error message. Mystartup.pyis :## Scripts Python GIS // import enableFeatCountAndAlphaSlider # // enableFeatCountAndAlphaSlider.run()– kFly Aug 01 '19 at 14:45QgsProject.instance().legendLayersAdded.connect(enableFeatCountAndAlphaSlider)– Joseph Aug 01 '19 at 14:59enableFeatCountAndAlphaSlider.run()with yours, but the script does not activate when adding a new layer. I got this message from qgis log :Warning: QObject::connect: signal not found in QgisAppInterface. Not sure if it is connected. – kFly Aug 01 '19 at 15:17from qgis.core import QgsMapLayer, QgsProjectandfrom qgis.utils import ifaceat 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. – Joseph Aug 02 '19 at 09:10