1

This question is a follow-up of this topic: Setting Alpha Slider and Feature Count ON by default using QGIS?

I am trying to run this script for each new project I open.

But I am failing of making it active.

From my knowledge of QGIS, a script called startup.py should be created.

In QGIS 3, this script is there

[Linux] /$HOME/.local/share/QGIS/QGIS3/profiles/default/python

[Windows] C:\Users\You\AppData\Roaming\QGIS\QGIS3\profiles\default\python\startup.py

(You can check this directory under : Settings > User Profile > Open Active Profile Directory)

My startup.py script is :

from qgis.core import QgsMapLayer
from qgis.core import QgsProject
from qgis.utils import iface
import enableFeatCountAndAlphaSlider    #Activation sub

QgsProject.instance().legendLayersAdded.connect(enableFeatCountAndAlphaSlider)

The routine enableFeatCountAndAlphaSlider is stored in the same directory as startup.py and as the following content:

def enableFeatCountAndAlphaSlider(layers):
# By Joseph (https://gis.stackexchange.com/questions/329919/qgis-3-4-setting-alpha-slider-and-feature-count-on-by-default/329984#329984)
    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)

I have got no message from qgis windows. When running manually startup.py from the console I have got the following error message:

QgsProject.instance().legendLayersAdded.connect(enableFeatCountAndAlphaSlider) NameError: name 'QgsProject' is not defined

What am I missing and how do I correct it?

  • you need import QgsProject from qgis.core import QgsProject – Fran Raga Aug 3 at 20:33
  • 2
    your question's title is a bit misleading since you know how to start a script using startup.py ... – snaileater Aug 4 at 13:20
  • @FranRaga: already done at line 2, in startup.py. – kFly Aug 4 at 13:34
1

Your Windows path is incorrect, it should be:

C:/Users/You/AppData/Roaming/QGIS/QGIS3/startup.py

This would explain why you are not receiving any error messages because you should be. If you have a small number of simple functions, it might be better to store them all in your startup.py so you can just load this single file:

from qgis.core import QgsMapLayer, QgsProject
from qgis.utils import iface

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())

QgsProject.instance().legendLayersAdded.connect(enableFeatCountAndAlphaSlider)

However, if you want to store this function in a separate file in the same directory as startup.py and load it via this startup file, then add the following to startup.py:

import sys, os
from qgis.utils import iface
sys.path.append(os.path.dirname(os.path.abspath(__file__)))

import enableFeatCountAndAlphaSlider
  • I am running QGIS on a Linux OS. According to this qgis doc, the directory is to be correct (QGIS3.4?): qgis.org/test/en/docs/pyqgis_developer_cookbook/…. However I suspect the startup.py is not read because I dont see any message in terminal when I launch qgis. I don't know how to check or force the reading ? This question seems related but has no solution yet: gis.stackexchange.com/questions/296504/… – kFly Aug 11 at 16:59
  • @kFly - So you tested this path on Linux :/$HOME/.local/share/QGIS/QGIS3/startup.py? – Joseph Aug 12 at 13:09
  • Yes. I a also put all the commands in startup.py. I also tryed with a buggy script. Script is not active in a new project, and no error/message is shown in the lauching console. – kFly Aug 13 at 20:53
  • @kFly - Perhaps you should ask a new question concerning the correct path for a Linux system for startup scripts. – Joseph Aug 14 at 10:22
  • @kFly - Have you tried the method posted here where you could enter QStandardPaths.locateAll( QStandardPaths.AppDataLocation, "startup.py" ) into the Python Console and if it returns a path then it is correct, if it returns an empty list then the path is incorrect. – Joseph Aug 14 at 10:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.