1

I want to add a layer in QGIS that is editable and has Add Polygon Feature selected (by default). I can easily accomplish the editable part by this command:

layer = QgsVectorLayer("Polygon?crs=epsg:4326", "temporary_points", "memory")
layer.startEditing()
#Something more here?
QgsProject.instance().addMapLayer(layer)

A bonus question is why it takes extremely long time to get the saving popup when I have that as a part of my code? (and none when I run it from the Python Console)

2
  • Not sure about your bonus question - what do you mean the saving popup? Can you edit your question to show this line of code?
    – 15Step
    Commented Apr 2, 2018 at 13:53
  • 2
    The "problem" was that I declared the variable self.layer = QgsVectorLayer(...) in that part of the code. The solution was to make no changes in that part but just add a self.layer = None in the init function.
    – axel_ande
    Commented Apr 2, 2018 at 15:26

1 Answer 1

4

Here's a way to select Add Polygon feature programmatically:

layer = QgsVectorLayer("Polygon?crs=epsg:4326", "temporary_points", "memory")
layer.startEditing()
qgis.utils.iface.actionAddFeature().trigger() #Yep, something here.
QgsProject.instance().addMapLayer(layer)

Your Answer

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

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