[PyQt] Keyboard shortcut for specific widget

Joe Borġ mail at jdborg.com
Mon Sep 2 10:00:49 BST 2013


I'm trying to use QShortcut to set a keyboard shortcut, only for when a
certain widget is focused.

I thought that by setting the parent, this would work. For example:

QtGui.QShortcut(QtGui.QKeySequence("Ctrl+Return"), self.myWidget,
self.doSomething)

But, this is fired when ever Ctrl+Return is pressed. I know I can do this
by subclassing QWidget and overwriting setShortcut, but this seems overkill?

Is there an easier way?

I think this can be done using QShortcutContext
http://pyqt.sourceforge.net/Docs/PyQt4/qshortcut.html but

QtGui.QShortcut(QtGui.QKeySequence("Ctrl+Return"), self.myWidget,
self.doSomething, context=QtCore.Qt.WidgetShortcut)

doesn't work either.

Thanks,
Joseph David Borġ
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.riverbankcomputing.com/pipermail/pyqt/attachments/20130902/783004c0/attachment.html>


More information about the PyQt mailing list