2

I'm going to know how I can get layers full path in table of contents in QGIS3.4 when my QGIS is closed and it's running such as daemon?

Rememebr: my shapefiles are in some different folders.

enter image description here

This below is my code:

# If you are not inside a QGIS console you first need to import
# qgis and PyQt classes you will use in this script as shown below:
from qgis.core import *
import os
#This is is needed in the pyqgis console also
from qgis.core import (
    QgsVectorLayer
)
QgsApplication.setPrefixPath("/usr", True)
# Create a reference to the QgsApplication. Setting the
# second argument to False disables the GUI.
qgs = QgsApplication([], False)
qgs.initQgis()
layersName =[]

# Get the project instance
project = QgsProject.instance()
project.read('/media/sigma/Data/PyQGIS/pyqgis_project/pyqgis_hydro.qgz')

#print(project.fileName())

for Layers in QgsProject.instance().mapLayers().values():

    layersName.append(Layers.name())


path_to_shape_layers_in_toc =[]
for l in layersName:

    path_to_shape_layers_in_toc.append(os.path.join(QgsProject.instance().homePath(),l+".shp"))

vlayer = QgsVectorLayer(path_to_shape_layers_in_toc[2],"Countour Layers","ogr")

if not vlayer.isValid():
    print("Layers not loaded!")

I expect exact path shown in properties window for each layers in TOC, but with this code I get only layers path loaded in TOC, not full path.

  • If you already have a QgsMapLayer (e.g., a QgsVectorLayer), use the source method: my_layer.source() to get the path of your layers or the connection string, if it's a layer coming from a DB. – Germán Carrillo Sep 7 '19 at 13:36
  • Actually I didn't find a solution in QGIS 3.x documents and developer cookbook also – Predator X Sep 9 '19 at 17:46

Your Answer

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

Browse other questions tagged or ask your own question.