4

I'm using the following code that displays a window to create a new Shapefile:

from qgis.gui import QgsNewVectorLayerDialog
new_vector_layer_dialog = QgsNewVectorLayerDialog()
new_vector_layer_dialog.show()

How can I put a flag on the Polygon by code, and not the line or the point?

Also when you press the OK button, how to get the dialog to select where to save the Shapefile?

When you click OK, it does not create a Shapefile, but just closes the dialog box. Additionally, it does not create a vector layer in the Layers Panel.

What do I need to do to create the layer and load it into the Layers Panel?

enter image description here

enter image description here

Germán Carrillo
37.3k5 gold badges127 silver badges182 bronze badges
asked Apr 4, 2017 at 8:29
3
  • Welcome to GIS:SE @vazagena! Are you wanting to do this from a plugin or from the python console? Commented Apr 4, 2017 at 8:54
  • It is desirable and there and there, since in the beginning the task is to get acquainted, and then make a button in the module Commented Apr 4, 2017 at 9:36
  • If you can tell via the console python please in qgis Commented Apr 4, 2017 at 10:15

1 Answer 1

1

You need to call the static method runAndCreateLayer instead of show:

from qgis.gui import QgsNewVectorLayerDialog
layerPath = QgsNewVectorLayerDialog().runAndCreateLayer()
if layerPath:
 layer = QgsVectorLayer(layerPath, 'new layer', 'ogr')
 QgsMapLayerRegistry.instance().addMapLayer(layer)

Concerning the Polygon radio button, the dialog doesn't seem to let you do that. You could do something like this:

myDialog.findChild( QRadioButton, 'mPolygonRadioButton').setChecked(True)

Which will be nonetheless overwritten when calling runAndCreateLayer.

Note: The QgsNewVectorLayerDialog class for QGIS 3 lets you set a CRS. Setting geometry type could be a feature request for QGIS.

answered Apr 4, 2017 at 12:32

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.