Subclass QLineEdit and override eventFilter() something like this:
bool TranslatingEdit
::eventFilter( QObject * watched,
QEvent * event
) {
TranslatingEdit *edit = qobject_cast<TranslatingEdit *>(watched);
if ( edit
&& event
->type
() == QEvent::KeyPress ) { QKeyEvent *k
= static_cast<QKeyEvent
*>
( event
);
// your mapping logic here this is an example for a single key
if ( k->key() == Qt::Key_Space) {
qApp->postEvent(edit, evtSpace );
return true; // this stops the original event here
}
}
}
bool TranslatingEdit::eventFilter( QObject * watched, QEvent * event )
{
TranslatingEdit *edit = qobject_cast<TranslatingEdit *>(watched);
if ( edit && event->type() == QEvent::KeyPress ) {
QKeyEvent *k = static_cast<QKeyEvent *>( event );
// your mapping logic here this is an example for a single key
if ( k->key() == Qt::Key_Space) {
QEvent *evtSpace = new QKeyEvent(QEvent::KeyPress, Qt::Key_Underscore, Qt::NoModifier, "_");
qApp->postEvent(edit, evtSpace );
return true; // this stops the original event here
}
}
return QLineEdit::event(event);
}
To copy to clipboard, switch view to plain text mode
Then install this filter on the widget itself in its constructor:
installEventFilter(this);
installEventFilter(this);
To copy to clipboard, switch view to plain text mode
The filter code can be in any QObject , for example a parent form class, if that is more convenient.
Edit: Actually, in hindsight, this might lead to an infinite loop if you map a key to another key that is also subject to mapping.
Bookmarks