[GIS] QGIS custom action and button toggle

pythonqgis

I've added a custom toolbar with some buttons to my QGIS 2 plugin. I'm wondering how to get the button toggled (pushed in-looking effect) using Python. I've gotten this to work for existing buttons like so:

iface.actionAddFeature().trigger()

I can't seem to figure out how to apply it to my own actions and buttons.

Best Answer

When you create action for button add:

self.myaction.Checkable(True)

In action callback, when you set your tool as active, toggle button on:

self.iface.mapCanvas().setMapTool(self.myTool)
self.myaction.setChecked(True)

Add tool deactivation callback, which toggle button off:

def deactivate(self):
    self.myaction.setChecked(False)

And connect it somewhere in init code:

self.iface.mapCanvas().mapToolSet.connect(self.deactivate)
Related Question