I am trying to create a plugin that has a QgsMapCanvas of itself. The question is that I can't seem to find the appropriate library to use polygon select feature in the QgsMapCanvas. I know that iface has actionSelect().trigger() function to implement the required but that allows selection only over the QGIS window and not on the plugin's map canvas.


Found a workaround to it. To implement iface.actionSelect().trigger() map tool in QgsMapCanvas, I created a class to initialize QgsMapToolIdentifyFigure

from qgis.gui import QgsMapCanvas,QgsMapToolIdentifyFeature
class initLayerFeatureSelectionTool(QgsMapToolIdentifyFeature):
    def __init__(self, canvas):
        self.canvas = canvas
        QgsMapToolIdentifyFeature.__init__(self, self.canvas)
    def keyPressEvent( self, e ):
        pass # disable escape key event to prevent loosing maptool
mapCanvas=QgsMapCanvas() # initialize map canvas
selectLayerFeatureSelectTool=initLayerSelectFeatureSelectionTool(mapCanvas) # initalize QgsMapToolIdentifyFeature
mapCanvas.setMapTool(selectLayerFeatureSelectTool) # set canvas map tool    

def loadCoordinatesInPlugin(feature):
    """ to allow multiple selection remove first line of this function"""
    VectorLayer.select(feature.id()) #select feature in vector layer
| improve this answer | |

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.