2

I want to make a pyqgis module that will perform an action similar to Identify tool. For example there is a tiny qt widget with a text box that is opened until user decide to close it manually. When user selects feature(s) of some layer, script immediately reads feature's attributes and print it in a text box. As soon as user selects another feature, widget's text will also change according to current selection. Is there some onSelectionChange() method for such actions?

1

Yes, there definately is,

In initGui in one of my plugins, I have

self.iface.mapCanvas().selectionChanged.connect(self.markselected)

Then in markselected, I do: (self.view.layer is defined elsewhere)

def markselected(self):
    if self.view.layer==None: 
        return
    try:
        sels=self.view.layer.selectedFeatures() # The selected features in the active (from this plugin's point of view) layer
        n=len(sels)
        if n>0:
            for sel in sels:
               (process selected items)       

Works like a charm.

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.