8

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.

7
  • Do you want to get info from selected features or identified features ? Commented Nov 1, 2017 at 8:11
  • @HugoRoussaffa-GeoDatup The identified feature as i mention in my question
    – abd0991
    Commented Nov 1, 2017 at 11:42
  • 1
    you wrote > so i can get some information from the selected feature and show it in a panel within my plugin. it's confusing. So you need to get signal from identify feature base on the standard IdentifyTool accessible on Canvas bar button, isn't it ? Commented Nov 1, 2017 at 11:46
  • @HugoRoussaffa-GeoDatup Yes, what you said is what i want. And sorry for the confusion, i will edit my question.
    – abd0991
    Commented Nov 1, 2017 at 11:49
  • problem is you create a new QgsMapToolIdentifyFeature with a connection. this is work as it's show on followed answer. But you need to catch signal from feature(s) that are identifying. You might to follow this answer to get what you want (sub-classed the existing identify tool and connect to it) : gis.stackexchange.com/a/130642/53945 Commented Nov 2, 2017 at 11:32

2 Answers 2

7
+25

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

6
  • 2
    Just to note, to work in the plugin the statements need to be included within the run() function.
    – artwork21
    Commented Oct 31, 2017 at 12:51
  • Sorry for the delayed response, i tested the code above with QGIS 2.18.13 but didn't worked.
    – abd0991
    Commented Nov 4, 2017 at 8:21
  • 1
    Sorry but this code works perfectly with QGIS 2.18.13
    – Fran Raga
    Commented Nov 4, 2017 at 10:39
  • @FranciscoRaga Hmm, your code create new QgsMapToolIdentifyFeature map tool, i tested it again and it worked, sorry but i want to catch the signal from the actual IdentifyTool which exists in the QGIS toolbar not create new one. Sorry maybe my question is confusing.
    – abd0991
    Commented Nov 4, 2017 at 13:03
  • Maybe you question is gis.stackexchange.com/q/243502/49538 ?
    – Fran Raga
    Commented Nov 4, 2017 at 13:38
2

To stick with your code :

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

def run(self):
    mCanvas = self.iface.mapCanvas()
    f = QgsMapToolIdentifyFeature(mCanvas, self.iface.activeLayer())
    mCanvas.setMapTool(f)
    f.featureIdentified.connect(self.onFeatureIdentified)
3
  • just a note, for to stick the question code need put mCanvas = self.iface.mapCanvas()
    – Fran Raga
    Commented Oct 31, 2017 at 22:08
  • Yep right! I miss that
    – SIGIS
    Commented Oct 31, 2017 at 23:11
  • Sorry for the delayed response, i tested the code above also with QGIS 2.18.13 but didn't worked.
    – abd0991
    Commented Nov 4, 2017 at 8:21

Your Answer

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

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