I have created a push button, when it is clicked it calls the function 'selectfeatures'. This function will call the Select feature by free hand tool of QGIS and allows the user to select features in the mapcanvas. What I need is my event to recognize the features selected in mapcanvas and call another function. How should this be accomplished?
Asked
Active
Viewed 3,493 times
1 Answers
13
If I got you right, you can use the SIGNAL selectionChanged from your vector layer and connect it to your other function (which must accept an argument to receive selected features' ids).
For example, load a vector layer to QGIS, activate it in the ToC and run the following code in the QGIS Python console. You should see the function is running after a new selection, recognizing the selected features' ids.
lyr=iface.activeLayer()
def myFunction(selFeatures):
print str(len(selFeatures)) + " features were selected: " + str(selFeatures)
lyr.selectionChanged.connect(myFunction)
Germán Carrillo
- 36,307
- 5
- 123
- 178
myFunctionabove) and in the function you create or get your layer define the SIGNAL-SLOT connection. As simply as that. However, be careful with setting the connection several times (e.g., each time you call a dialog in your plugin, see https://gis.stackexchange.com/questions/137160/odd-behavior-in-a-qgis-plugin-my-function-is-triggered-twice/137161#137161) – Germán Carrillo Feb 13 '19 at 03:36