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.