[PyQt] Disabling a key event

David Boddie dboddie at trolltech.com
Thu Oct 18 14:56:42 BST 2007


On Thu Oct 18 03:22:32 BST 2007, Gustavo A. Díaz wrote:

> What i want to do this time, is to disable a key event in my app. For
> example i want to avoid closing the app by pressing ALT + F4:
> 
> def keyPressEvent(self, event):
>         if event.key() == QtCore.Qt.Key_F4 and (event.modifiers() &
>         QtCore.Qt.AltModifier):
>             # DO Nothing. How?
> 
> So, which action or code i should implement in place of "# DO Nothing. How?"

If you want to stop key events from being passed on to other widgets, just
accept the key event with event.accept().

If you want to intercept events at a higher level, you'll need to start
looking at event filters. The event model is described in detail in a Qt
Quarterly article from 2004:

  http://doc.trolltech.com/qq/qq11-events.html

On the other hand, if you just want to stop the window from being closed by
the window system, you should reimplement closeEvent() in your main window
and explicitly ignore the event passed to it:

  def closeEvent(self, event):
      event.ignore()

Hope this helps,

David
-- 
David Boddie
Lead Technical Writer, Trolltech ASA



More information about the PyQt mailing list