[PyQt] QLineEdit vs. menu keyboard shortcuts

Nathan Weston nathan at genarts.com
Wed Jun 29 14:50:58 BST 2011

On 6/28/2011 2:17 PM, Hans-Peter Jansen wrote:
> On Tuesday 28 June 2011, 19:56:35 Nathan Weston wrote:
> If you're subclassing QLineEdit anyway, what does stop you from
> overriding keyPressEvent to catch and accept() any unwanted key
> presses?

I can certainly do that, but it doesn't cause my QActions to trigger. I 
could intercept specific shortcuts and manually trigger the QActions, 
but then my QLineEdit subclass has to know about all the menu items, 
which is pretty ugly.

The interaction between QAction shortcuts and widget-specific shortcuts 
is a little strange. I've written a little test program: it's a 
QMainWindow with some menu items and a QLineEdit subclass as the central 

The first menu item has Ctrl+R as its shortcut, which doesn't clash with 
anything in QLineEdit. If I type Ctrl+R, the action intercepts the 
keypress event *before* QLineEdit gets it -- I never see a keyPressEvent.

The menu item shortcut has Ctrl+Z as a shortcut. If I type Ctrl+Z, the 
QLineEdit gets a keyPressEvent, which causes an undo of the text. If I 
ignore() the event, it disables the undo behavior, but my QAction still 
doesn't fire.

I've attached my program. If you press Ctrl+Z it will print 'Foo 
keyPress 16777249' (for the Ctrl) and 'Foo keyPress 90' (for the Z). If 
you press Ctrl+R it just prints 'action' from the slot connected to my 

Does anyone understand what's going on under the hood here?

. . . . . . . . . . . . . . . . . . . . . . . . .
Nathan Weston                   nathan at genarts.com
GenArts, Inc.                   Tel: 617-492-2888
955 Mass. Ave                   Fax: 617-492-2852
Cambridge, MA 02139 USA         www.genarts.com

-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: test.py
URL: <http://www.riverbankcomputing.com/pipermail/pyqt/attachments/20110629/9aefd578/attachment.ksh>

More information about the PyQt mailing list