My program opens a QGIS project, loads one layer, adds a point on this layer and then stops. My problem is - I don't save the project explicitly, but the added points are always saved (I have to remove them manually).
The question seems to be duplicate of How close project QGIS (* .qgs) without saving (PyQGIS), but the answer from there (project.clear()) - I cannot import qApp from PyQt5.QtGui, but processing the user interface is probably not important there. I just added project.clear() before qgs.exitQgis(), but the project is still saved. It's QGIS version 3.30 on Windows 10.
Commentators requested the code, so here is my code (without all the imports; I am running this with standalone PyQGIS):
# Supply path to qgis install location
QgsApplication.setPrefixPath("C:\OSGeo4W", True)
Create a reference to the QgsApplication. Setting the
second argument to False disables the GUI.
qgs = QgsApplication([], False)
Load providers
qgs.initQgis()
project.read('C:\Users\Tambet\Desktop\Barcelona qgis\planols1.qgs')
layers = QgsProject.instance().mapLayersByName('test')
print(layers[0].name())
layer = layers[0]
caps = layer.dataProvider().capabilities()
if caps & QgsVectorDataProvider.AddFeatures:
print("Adding feature..")
feat = QgsFeature(layer.fields())
feat.setAttributes([0, 'added programatically'])
feat.setGeometry(QgsGeometry.fromPointXY(QgsPointXY(431613.6, 4582819.9)))
res, outFeats = layer.dataProvider().addFeatures([feat])
features = layer.getFeatures()
for feature in features:
# retrieve every feature with its geometry and attributes
print("Feature ID: ", feature.id())
# fetch geometry
# show some information about the feature geometry
geom = feature.geometry()
geomSingleType = QgsWkbTypes.isSingleType(geom.wkbType())
if geom.type() == QgsWkbTypes.PointGeometry:
# the geometry type can be of single or multi type
if geomSingleType:
x = geom.asPoint()
print("Point: ", x)
layoutmanager = project.layoutManager()
print(layoutmanager.layouts())
layout = layoutmanager.layoutByName("test1") #Layout name
exporter = QgsLayoutExporter(layout)
print(exporter.exportToPdf("C:\Users\Tambet\eclipse-workspace\qgis test\test.pdf", QgsLayoutExporter.PdfExportSettings() ))
project.clear()
qgs.exitQgis()
dataProviderto add the point? Could you please share the code you use doing things you mentioned? It is hard to say something without seeing your script. – Kadir Şahbaz Apr 03 '23 at 09:19