PDA

View Full Version : QLineEdit vs ApplicationShortcut



Kryzon
17th August 2015, 23:07
I have a QAction with a shortcut set to letter "x", and the shortcut context Qt::ApplicationShortcut (at any point in the application the shortcut is available).
When I click on a QLineEdit to start writing some text, all letters except "x" are read by this widget. The "x" will just activate the QAction.

How can I use an application-wide shortcut like that in a way that makes it only available when the user isn't writing on a QLineEdit or other widget that receives text?

Kryzon
18th August 2015, 20:26
To answer my own question, if you reimplement the "bool event( QEvent* event )" handler, you can catch QEvent::ShortcutOverride events that represent shortcut sequences that you want to interpret differently in the widget.

Using this solved the problem:

bool CustomLineEdit::event( QEvent* event )
{
switch( event->type() )
{
case QEvent::ShortcutOverride:
event->accept();
return true;
break;

default: // Important to avoid compiler warnings about unhandled QEvent types.
return QLineEdit::event( event );
break;
}
}
Now the widget accepts "x" as text input when it has keyboard focus, and when it doesn't the QAction is invoked instead.
More information here: http://wiki.qt.io/ShortcutOverride