1

Code snippet:

from qgis.core import QgsApplication, QgsProject, QgsVectorLayer
import os
os.environ["PROJ_LIB"]="/Applications/QGIS.app/Contents/Resources/proj"
app = QgsApplication([], False)

# Path to QGIS project
project = QgsProject.instance()
project.read("path to qgis project")

# Path to layer
path_to_layer = "path to shapefile"
vlayer = QgsVectorLayer(path_to_layer, "layer", "ogr")
QgsProject.instance().addMapLayer(vlayer)

project.write()

I am developing a standalone PyQGIS script on Mac OS. The purpose of the script is to load layers to a QGIS project that is already open in an instance of the QGIS application. It all works fine except that the added layers do not show up under Layers in QGIS. If I close the QGIS project and open it again, all the added layers are visible. How can I make the layers visible without having to close the QGIS project and reopen? I assume I somehow have to refresh the QGIS project but can't figure out how to do it.

5
  • 1
    You may need to provide a larger code snippet in your question, or edit the body of the question. You have asked about loading layers but your code shows adding a layer group. Apr 16 at 1:54
  • Really tired last night so a bit out of focus. I have updated with a code that can be run.
    – Pro Vebbis
    Apr 16 at 5:23
  • 2
    Just so we're clear, your PyQGIS script is standalone. But QGIS is already open, with the project already loaded? That's not specifically mentioned in the question. Apr 16 at 6:42
  • Yes, that is correct. PyQGIS script is standalone. QGIS is already open and I want they layer to automatically load in QGIS when running the PyQGIS script.
    – Pro Vebbis
    Apr 16 at 6:45
  • My guess is that it is going to be tricky. Keep in mind that you can have any number of open QGIS instances at a time. I'm not seeing a mechanism for communicating with an already open QGIS (ie one that wasn't opened by script in the first instance). Apr 16 at 8:44

1 Answer 1

0

Your opened QGIS application is completely independent from the QGIS application spawned by your script. They are separate processes with their own Python environment etc.

They also both have a unique QgsProject object instance, loaded from the same file path (apparently), but completely independent during their lifecycle.

The reason why changes appear when you save the project in one application and re-open it in the other, is that in that moment the newly saved file will be read.

To communicate between multiple applications you would need to use something more sophisticated like sockets, websockets etc.

If refreshing the project in QGIS A when the file is saved by QGIS B is enough, check out QFileSystemWatcher.

3
  • Will you get one unique QgsProject by e.g. open the project in QGIS from the standalone script? If so, do you know how to open a project in QGIS from standalone script? I found a way to open QGIS but not open a project. This will open QGIS: process = subprocess.Popen("path to QGIS installation", shell=True, stdout=subprocess.PIPE).
    – Pro Vebbis
    Apr 17 at 6:10
  • You mean you want to launch QGIS and immediately open a project? "path to QGIS executable" "path to project file" would do that. This will launch a new QGIS though, it will not "remotely control" a running instance. Apr 17 at 8:53
  • 1
    Thanks. As you point out, I will not be able to remotely control it so subprocess.Popen doesn't really help with what I am after.
    – Pro Vebbis
    Apr 17 at 14:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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