[PyQt] QLineEdit vs. menu keyboard shortcuts

Nathan Weston nathan at genarts.com
Wed Jun 29 15:39:15 BST 2011


>> from PyQt4.QtCore import *
>> from PyQt4.QtGui import *
>> import sys
>>
>> class Foo(QLineEdit):
>>      def keyPressEvent(self, event):
>>          print 'Foo keyPress', event.key(); sys.stdout.flush()
>>          QLineEdit.keyPressEvent(self, event)
>>
>> app = QApplication([])
>> window = QMainWindow()
>> f = Foo()
>> window.setCentralWidget(f)
>> window.show()
>>
>> def do_action():
>>      print 'action'; sys.stdout.flush()
>> action1 = QAction('Action 1', f, triggered=do_action)
>> action1.setShortcut(Qt.CTRL + Qt.Key_Z)
>> action1.setShortcutContext(Qt.WidgetShortcut)
>
> For actions that should work all over the mainwindow, you should use
> Qt.WidgetWithChildren here.
>

Oops, that line was an artifact of an earlier experiment. I actually 
tried every possible value for shortcutContext, none of them seem to 
make a difference.


More information about the PyQt mailing list