2

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?

kFly
  • 859
  • 1
  • 11
  • 27

1 Answers1

2

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
Joseph
  • 75,746
  • 7
  • 171
  • 282
  • I am running QGIS on a Linux OS. According to this qgis doc, the directory is to be correct (QGIS3.4?): https://qgis.org/test/en/docs/pyqgis_developer_cookbook/intro.html#running-python-code-when-qgis-starts. 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: https://gis.stackexchange.com/questions/296504/startup-py-in-qgis-not-executed – kFly Aug 11 '19 at 16:59
  • @kFly - So you tested this path on Linux :/$HOME/.local/share/QGIS/QGIS3/startup.py? – Joseph Aug 12 '19 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 '19 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 '19 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 '19 at 10:46