Monday, 8 May 2017

pyqgis - Get the identified feature


I use QgsMapToolIdentifyFeature to trap the featureIdentified signal, so i can get some information from the identified feature and show it in a panel within my plugin.



here is the code, my problem is the connection between the featureIdentified signal and my slot is not works:


def onFeatureIdentified(self, ff):
print("feature selected")

def run(self):
f = QgsMapToolIdentifyFeature(self.iface.mapCanvas(), self.iface.activeLayer())
f.featureIdentified.connect(self.onFeatureIdentified)

i don't know that's the problem in my code since it doesn't raise any error.



Answer




This is a simple code for connect your QgsMapToolIdentifyFeature signal.


from qgis.gui import QgsMapToolIdentifyFeature

def onFeatureIdentified(feature):
print "feature selected : "+ str(feature.id())

mapTool = None
mc=iface.mapCanvas()
lyr=iface.activeLayer()
mapTool = QgsMapToolIdentifyFeature(mc)

mapTool.setLayer(lyr)
mc.setMapTool(mapTool)
mapTool.featureIdentified.connect(onFeatureIdentified)

And when click in a feature print the id.


enter image description here


Regards


Tested with QGIS 2.18.14 on W10




UPDATE: Add GIF with QGIS 2.18.13



enter image description here


No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...