I want to create a QGIS Plugin in which the user is clicking on the map to get the coordinates which are used to create a point feature. However until now the point feature is not shown in the map. Here is the code I use:
le_koord = self.dlg.lineedit_Koordinate.text()
try:
le_koord = [float(coord.strip()) for coord in le_koord.split(',')]
except:
QMessageBox.critical(self.iface.mainWindow(),
'Fehler bei der Eingabe!',
"Es müssen durch Komma getrennte Koordinaten Lat Long eingegeben werden!..")
return
x_koord = float(le_koord[0])
y_koord = float(le_koord[1])
layer_out = QgsVectorLayer("Point?
crs="+str(project.crs())+"field=address:string&field=license:string",
"Lage",
"memory")
caps = layer_out.dataProvider().capabilities()
if caps & QgsVectorDataProvider.AddFeatures:
feature = QgsFeature(layer_out.fields())
point_out = QgsPointXY(x_koord, y_koord)
feature.setGeometry(QgsGeometry.fromPointXY(point_out))
(res, outFeats) = layer_out.dataProvider().addFeatures([feature])
However, I only managed to make the point visible if I use project.addMapLayer(layer_out)
, but as this creates always a new version of the selected layer, it is not usable. I found this tutorial in which the same commands are used, but which is done directly in the Python console in QGIS.
I'm using QGIS 3.30 and Python 3.9 on Ubuntu 22.04.2 LTS.
with edit(layer)
context instead of going low-level through the data provider. Or calllayer.triggerRepaint()
when you are done.~~ Actually, the code does not show the layer being part of the project at all.layer_out.triggerRepaint()
but the feature is not appearing in the map. I want to create several points (later with distinct attributes) to the layer "Lage", as it is shown in the tutorial. Is this not possible, if I don't code directly in the Python Editor in QGIS? Thanks for the hint regarding QDoubleSpinBox, as soon I solved this problem, I will change it ;)