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()

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

  • Welcome to GIS:SE @vazagena! Are you wanting to do this from a plugin or from the python console? – Joseph Apr 4 '17 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 – vaza gena Apr 4 '17 at 9:36
  • If you can tell via the console python please in qgis – vaza gena Apr 4 '17 at 10:15

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')

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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