[Interest] Intercept key bindings for QTextEdit subclass
Volker Hilsheimer
volker.hilsheimer at qt.io
Tue Apr 1 22:11:39 CEST 2025
> On 1 Apr 2025, at 20:59, Josh <jnfo-d at grauman.com> wrote:
>
> Hi,
>
> I have a QTextEdit subclass that I want to intercept the key bindings for (like Ctrl-X, etc.). If I reimplement QTextEdit::keyPressEvent(QKeyEvent *k) I can get the keys and do whatever with them. But I actually want to ignore the event so it gets passed up and I can add it as a menu action item shortcut to the main window. But calling k->ignore(); and then returning in keyPressEvent() doesn't allow the main window to use the shortcut. Is there a way that the main window can see those shortcut keys?
>
> Josh
QEvent::ShortcutOverride is Qt’s mechanism to check if an application shortcut (like Ctrl+X in your case) is important enough for the currently focused widget to take precedence. I.e. if a text input widget has focus, the expectation is that the text input’s cut/copy/paste handling should override the application. So, those widgets accept the QEvent::ShortcutOverride in their event(QEvent *) override to signal to Qt that they want to handle that key combination themselves.
So, in your QTextEdit subclass, override event() and also ignore the QEvent::ShortcutOverride event (which is a QKeyEvent) for Ctrl+X.
Volker
More information about the Interest
mailing list