1

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

Alpha Slider

  • with Feature count activated

Layer Entity Count

PolyGeo
  • 65,136
  • 29
  • 109
  • 338
kFly
  • 859
  • 1
  • 11
  • 27

1 Answers1

1

You could use something like the following in the Python Console which:

  • Enables the feature count setting for vector-type layers
  • Enables the transparency slider for any layer

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)

enableFeatCountAndAlphaSlider

Joseph
  • 75,746
  • 7
  • 171
  • 282
  • 1
    Works directly for me. Thanks a lot for your detailed answer ! I am just beginning with pyScript, it helped a lot. – kFly Jul 26 '19 at 17:04
  • What do you use to create the animated picture you add in your answer ? – kFly Jul 26 '19 at 17:07
  • 1
    @kFly - Most welcome! Glad it helped :). I used ScreenToGif. – Joseph Jul 30 '19 at 09:52
  • Do you how I can run your script automatically when opening a new project ( [ QGIS3]? I thought in was in startup.py but I did not manage to make it work for the moment. – kFly Aug 01 '19 at 14:14
  • @kFly - Did you put the script in C:\Users\You\AppData\Roaming\QGIS\QGIS3\profiles\default\python\startup.py? When you and you run QGIS, you might receive errors such as module x is not defined etc. 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
  • On linux the directory should be: /$HOME/.local/share/QGIS/QGIS3/profiles/default/python I do not get any error message. My startup.py is : ## Scripts Python GIS // import enableFeatCountAndAlphaSlider # // enableFeatCountAndAlphaSlider.run() – kFly Aug 01 '19 at 14:45
  • 1
    @kFly - You don't run the function manually, you connect it to a signal which is emitted whenever a layer is added to QGIS which is what this line does: QgsProject.instance().legendLayersAdded.connect(enableFeatCountAndAlphaSlider) – Joseph Aug 01 '19 at 14:59
  • I replaced my last line enableFeatCountAndAlphaSlider.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:17
  • 1
    @kFly - I tested the script as a startup. I had to include the lines 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. – Joseph Aug 02 '19 at 09:10
  • Many thanks Joseph. Script is still not active at startup. I wrote a separate subject, to be able to format the text: https://gis.stackexchange.com/questions/330967/qigs-running-a-script-when-opening-any-project – kFly Aug 03 '19 at 17:19