[PyQt] QLineEdit vs. menu keyboard shortcuts

Hans-Peter Jansen hpj at urpla.net
Tue Jun 28 19:17:18 BST 2011


On Tuesday 28 June 2011, 19:56:35 Nathan Weston wrote:
> On 6/28/2011 1:48 PM, Baz Walter wrote:
> > On 28/06/11 16:50, Nathan Weston wrote:
> >> QLineEdit has some keyboard shortcuts (e.g. Ctrl+Z for Undo) that
> >> clash with the shortcuts for QActions in my application menus.
> >> When a QLineEdit has keyboard focus, it intercepts these shortcuts
> >> and my QActions are never triggered.
> >>
> >> I'd like to give application-level shortcuts precedence over
> >> QLineEdit's shortcuts, but I can't seem to find a clean and
> >> general way to do this.
> >>
> >> Alternatively, is there a way to customize the keyboard shortcuts
> >> in QLineEdit? If I could disable some of the problematic shortcuts
> >> that would be a reasonable workaround.
> >
> > the default key bindings for QLineEdit are hard-coded, but the
> > actual key sequences used are platform-dependent (and the same goes
> > for most (all?) other widgets which have them).
> >
> > but what is your reason for wanting to change the default
> > behaviour?
> >
> > as a user, i would expect all the 'normal' shortcuts to work when
> > typing in a line edit. if Ctrl+Z undid something in some other
> > widget that i wasn't looking at, it could become very confusing.
> > (IIRC this was how things used to work with qt3 - and good riddance
> > to that!).
>
> I have a subclass of QLineEdit for entering numbers. The user can
> either type in a number, or drag the mouse to increase/decrease the
> value, like a slider (similar widgets are often found in After
> Effects and other video editing software).
>
> In the latter mode, the widget is temporarily set as non-editable, so
> its built-in Undo behavior doesn't work at all (but it still eats the
> keyboard event!).

If you're subclassing QLineEdit anyway, what does stop you from 
overriding keyPressEvent to catch and accept() any unwanted key 
presses?

Pete


More information about the PyQt mailing list