1

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()
2
  • One of the ways is to add custom action of python type to your layer. Have you considered it?
    – RainForest
    Jul 22, 2022 at 5:54
  • @RainForest I'm not sure what that means or what that would look like.
    – tbob
    Jul 22, 2022 at 14:14

2 Answers 2

2

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

1
  • Ok interesting, I'll look into that, thank you. Actually I'm pretty happy with the popup method I'm using so I'll probably stick with that, but this is good info.
    – tbob
    Jul 25, 2022 at 13:25
0

I ended up going with a Qt popup, like this;

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:
        feature = [feature.mFeature['line_id'] for feature in found_features]
        label, ok = QInputDialog.getInt(self.plugin, 'Line Label', 'Enter Label:')
        if ok:  
            query = "UPDATE planning.line \
                    SET line_label = %s \
                    WHERE line.line_id = %s", (label, feature[0],)
            self.plugin.db_conn.query(query, fetch=False)
            self.plugin.final_lines.triggerRepaint()
            self.plugin.add_label_layer()
            self.plugin.move_layers()

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

But if anyone knows of a way to change the attribute directly (which would update the database) please let me know.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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