Code to add polygon layer when a button is pressed in my plugin:
# Add polygon layer
self.poly_layer = QgsVectorLayer("Polygon", "target_polygons", "memory")
# Set polygon color and opacity
myRenderer = self.poly_layer.renderer()
mySymbol1 = QgsFillSymbol.createSimple({'color':'255,0,0,50',
'color_border':'#000000',
'width_border':'0.6'})
myRenderer.setSymbol(mySymbol1)
iface.layerTreeView().refreshLayerSymbology(self.poly_layer.id())
QgsProject.instance().addMapLayer(self.poly_layer)
# Enter editing mode
self.poly_layer.startEditing()
iface.actionAddFeature().trigger()
After adding a few polygons, I have arrow buttons that should iterate through each feature and zoom in on it, this works for polygon layers that I import, and for point layers, but not for this homemade polygon layer. Is it because the polygons have no attributes? if I open the attribute table and try to click on each polygon I cannot select it (strangely, "select all" still works).
I also want to ask, is it possible to auto add atttributes in pyqgis so that whenever a polygon is digitized the window does not pop up to ask for the info. If I use this code then a popup will open with each polygon created and I want to avoid that:
pr = self.poly_layer.dataProvider()
pr.addAttributes( [ QgsField("name", QVariant.String)])
UPDATE: let me provide some more code for clarity. I have tried tow different approaches for committing changes to the polygons. one is by having an "ok" button when the user is done adding polygons that will do this code:
iface.mainWindow().findChild(QAction, 'mActionToggleEditing').trigger()
Even after pressing ok (which will prompt the user to save changes), I still cannot select polys from the attribute table or zoom to the selected one using my arrow buttons. Another approach is to auto commit changes after each polygon is created;
self.poly_layer.featureAdded.connect(self.feature_added)
def feature_added(self):
self.poly_features = [feature for feature in
self.poly_layer.getFeatures()]
self.TotalLabelPoly.setText(str(len(self.poly_features)))
self.current_selection_poly = len(self.poly_features)
#self.poly_layer.commitChanges()
iface.actionSaveEdits().trigger()
the last two lines of that function are what I have tried and neither seem to solve my issue, the commitChanges() function actually makes qgis crash. Here is the code where I attempt to zoom in on selected geometry:
def right_arrow_poly(self):
if self.current_selection_poly < len(self.poly_features):
self.current_selection_poly += 1
self.AttributeNumberPolyBox.setText(str(self.current_selection_poly))
self.poly_layer.selectByIds([self.current_selection_poly])
box = self.poly_layer.boundingBoxOfSelected()
iface.mapCanvas().setExtent(box)
iface.mapCanvas().refresh()
def left_arrow_poly(self):
if self.current_selection_poly > 1:
self.current_selection_poly -= 1
self.poly_layer.selectByIds([self.current_selection_poly])
box = self.poly_layer.boundingBoxOfSelected()
iface.mapCanvas().setExtent(box)
iface.mapCanvas().refresh()
self.AttributeNumberPolyBox.setText(str(self.current_selection_poly))
I use very similar functions for a different layer that has point geometry and it works great.