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.

abd0991
  • 452
  • 4
  • 18
  • Do you want to get info from selected features or identified features ? – Hugo Roussaffa Nov 01 '17 at 08:11
  • @HugoRoussaffa-GeoDatup The identified feature as i mention in my question – abd0991 Nov 01 '17 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 ? – Hugo Roussaffa Nov 01 '17 at 11:46
  • @HugoRoussaffa-GeoDatup Yes, what you said is what i want. And sorry for the confusion, i will edit my question. – abd0991 Nov 01 '17 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) : https://gis.stackexchange.com/a/130642/53945 – Hugo Roussaffa Nov 02 '17 at 11:32
  • @HugoRoussaffa-GeoDatup Thanks, i will take a look at this. – abd0991 Nov 04 '17 at 08:21
  • pueden actualizar el metodo para la version qgis 3.4? Gracias, Eddison [Translated] Can you update the method for version qgis 3.4? Thanks, Eddison – Eddison Araya Nov 19 '18 at 00:47

2 Answers2

7

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

Fran Raga
  • 7,838
  • 3
  • 26
  • 47
  • 2
    Just to note, to work in the plugin the statements need to be included within the run() function. – artwork21 Oct 31 '17 at 12:51
  • Sorry for the delayed response, i tested the code above with QGIS 2.18.13 but didn't worked. – abd0991 Nov 04 '17 at 08:21
  • 1
    Sorry but this code works perfectly with QGIS 2.18.13 – Fran Raga Nov 04 '17 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 Nov 04 '17 at 13:03
  • Maybe you question is https://gis.stackexchange.com/q/243502/49538 ? – Fran Raga Nov 04 '17 at 13:38
  • @fransisco-raga look on the comment of the question and you will get more detail about the question. its not related to your link (your answer is but not the question) – Hugo Roussaffa Nov 09 '17 at 22:25
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)
SIGIS
  • 912
  • 7
  • 19