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
?
-
Welcome to GIS:SE @vazagena! Are you wanting to do this from a plugin or from the python console?Joseph– Joseph2017年04月04日 08:54:51 +00:00Commented 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 modulevaza gena– vaza gena2017年04月04日 09:36:37 +00:00Commented Apr 4, 2017 at 9:36
-
If you can tell via the console python please in qgisvaza gena– vaza gena2017年04月04日 10:15:24 +00:00Commented Apr 4, 2017 at 10:15
1 Answer 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.