4

I'm developing a QGIS plugin and I have created a custom tool. Once the process is over, if the user presses the ESCAPE key, I want my custom tool not to trigger mouse events on the map anymore.

How can I do that using python programming?

I have tried:

event.ignore()
3

According to this and to your previous question, you want to deactivate your tool by pressing the Escape key.

However, as far as I know, one cannot deactivate a map tool. One should activate another map tool to deactivate (indirectly) the current one. That is, the map canvas MUST always have an active map tool. So, in order to deactivate your map tool you must activate another one. For the sake of showing an example, I've chosen the Pan map tool.

If you were to activate the Pan map tool, you would use:

iface.actionPan().trigger()

So, let's say you want to activate the Pan map tool when pressing the Escape key, which would in turn deactivate your custom tool.

You can use the following code snippet to link the Escape key press event to the Pan map tool activation:

from PyQt4.QtGui import QShortcut, QKeySequence
from PyQt4.QtCore import Qt
shortcut = QShortcut(QKeySequence(Qt.Key_Escape), self.iface.mainWindow())
shortcut.setContext(Qt.ApplicationShortcut)
shortcut.activated.connect(self.iface.actionPan().trigger)

Since you are writing a QGIS plugin, you could put the code snippet into your plugin's main Python module (let's say main.py), for instance, in your initGui() method.

  • @gcarrillowhen i kept in initGui it doent work for me so i kept iface.actionPan().trigger() in key release event which is working fine.def keyPressEvent(self, event): if event.key() == QtCore.Qt.Key_Escape: print "Esc Pressed" pass def keyReleaseEvent(self, event): self.layer.removeSelection() qgis.utils.iface.actionPan().trigger() self.msgBar.pushMessage(" Process Completed.",level=QgsMessageBar.INFO) pass – user99 Feb 6 '15 at 10:05
  • Without activating the panMap tool is there anyway to close the custom tool? – user99 Feb 6 '15 at 13:01
  • @Pavidevi Your map must have an active tool anyway, if it's not yours, it should be another one. I chose Pan, you can choose whatever you want. – Germán Carrillo Feb 6 '15 at 14:29

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.