PyQGIS – Open Attribute Edit When Feature is Selected

pyqgisqgis-3qgis-plugins

For my plugin I want the user to be able to select a feature and an edit window popup for editing a certain attribute of that feature. I could do this by making a QInputDialog.getText show up when a feature is clicked. But I'm wondering if there is a more straight forward way to bring up an attribute edit box using pyqgis. Here is the class I am using as the selection tool:

class NumberTool(QgsMapToolIdentifyFeature):

    def __init__(self, plugin, iface, layer):
        self.plugin = plugin
        self.iface = iface
        self.canvas = self.iface.mapCanvas()
        self.layer = layer
        self.selected = []
        QgsMapToolIdentifyFeature.__init__(self, self.canvas, self.layer)
            
    def canvasPressEvent(self, event):
        found_features = self.identify(event.x(), event.y(), [self.layer], QgsMapToolIdentify.TopDownAll)
        if found_features:
            # open attribute edit box. how do this?
            self.layer.triggerRepaint()
            self.plugin.add_label_layer()
            self.plugin.move_layers()

    def deactivate(self):
        self.layer.removeSelection()

Best Answer

I mean something like this in my comment:

Create your own attribute edit form and select it in layer properties. Add your logic in python code.

enter image description here

Create new action for the layer.

enter image description here

New action "edit" is available in list of actions

enter image description here

Related Question