[PyQt] QLineEdit vs. menu keyboard shortcuts
Baz Walter
bazwal at ftml.net
Wed Jun 29 18:07:19 BST 2011
On 29/06/11 14:50, Nathan Weston wrote:
> 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
> widget.
>
> 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
> QAction.
>
> Does anyone understand what's going on under the hood here?
as was suggested earlier in this thread, if the line-edit doesn't have
the keyboard focus, it can't trigger its own shortcuts. if it *does*
have the focus, but the shortcut sequence is not one it handles, the
keypress event will be re-propagated (as happens with 'Ctrl+R' in your
example).
the simplest way to control the focus is to use clearFocus() and setFocus().
More information about the PyQt
mailing list