How can I replace every keypress character with my custom key in lineedit?
Hi,
I've my custom key maps table (A=c, B=f, C=d, ..... etc.)
I type and show the texts in lineedit normally,
but after checked a checkbox, I want to replace every letter immediately with my custom map letter.
eg. in unchecked mode I type ABCDE then I checked and keep type ABCDE, the letters in lineedit is as follow:
ABCDcfdj
I want to see and replace letter in lineedit's every key pressed.
In VB, this work is very easy. In Qt C++, how can I do it?
my sample vb code are:
Private Sub Text1_KeyPress(KeyAscii As Integer)
If ckMyMap.Value = 1 Then KeyAscii = KeyMap(KeyAscii)
End Sub
Public Function KeyMap(key As Integer) As Integer
'My Custom Key Map
If key = 65 Then
KeyMap = 99
ElseIf key = 66 Then
KeyMap = 102
ElseIf key = 67 Then
KeyMap = 100
ElseIf key = 68 Then
KeyMap = 106
ElseIf key ......
...............
...............
Else
KeyMap = key
End If
End Function
In Qt C++, how can I do it?
Thanks.
Re: How can I replace every keypress character with my custom key in lineedit?
Subclass QLineEdit and override eventFilter() something like this:
Code:
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
}
}
}
Then install this filter on the widget itself in its constructor:
Code:
installEventFilter(this);
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.
Re: How can I replace every keypress character with my custom key in lineedit?
You could try this (untested code):
Code:
// a new member variable (init to false in constructor)
bool m_translating;
// A getter
bool TranslatingEdit::isTranslating() const { return m_translating; }
// a setter/slot you can connect from your checkbox clicked(bool) signal
void TranslatingEdit::setTranslating(bool flag) { m_translating = flag; }
// and a protected override
void TranslatingEdit
::keyPressEvent( QKeyEvent * event
) {
if (m_translating) {
// your mapping logic here this is an example for a single key
if ( event->key() == Qt::Key_Space) {
QKeyEvent newEvent
(QEvent::KeyPress, Qt
::Key_Underscore, Qt
::NoModifier,
"_");
event->setAccepted(newEvent.isAccepted());
}
}
else {
}
}
Re: How can I replace every keypress character with my custom key in lineedit?
Re: How can I replace every keypress character with my custom key in lineedit?
The second option? Actually, I can see that in Line 22 in my second post does not need the "return" (possible compiler warning) and should call the base implementation with "event" not "newEvent" (would not compile).