[SOLVED] Subclassing QLineEdit and typing a special character when a key combination is pressed
-
Good afternoon.
I'm trying to subclass QLineEdit because I need to modify its behaviour pattern for few key combinations like Shift+Space and Shift+Return. I actually want it to type some special characters when one of these key combinations is pressed. For instance, when I press Shift+Space, I want my new LineEdit to type "■" (U+25A0) and, when I press Shift+Return, I want it to type "¶" (U+00B6).
My problem is I don't know which protected function I should modify or how I should modify it. Is it keyPressEvent, inputMethodEvent or another function? If I have to use or modify inputMethodeEvent, how should I use the class QInputMethodEvent?
Thank you in advance for your help.
-
this should do it:
@
void MyLineEdit::keyPressEvent(QKeyEvent* event)
{
if( event->modifiers() & Qt::ShiftModifier )
{
switch( event->key() )
{
case Qt::Key_Space:
this->insert( QString::fromUtf8("\u25A0") );
return;
break;
case Qt::Key_Return:
case Qt::Key_Enter:
this->insert( QString::fromUtf8("\u00B6") );
return;
break;
}
}QLineEdit::keyPressEvent(event);
}
@ -
raven-worx: don't forget to check for the Shift key modifier though...
-
[quote author="Andre" date="1375189703"]raven-worx: don't forget to check for the Shift key modifier though...[/quote]
i did...first line in the event handler ;) -
[quote author="raven-worx" date="1375189870"][quote author="Andre" date="1375189703"]raven-worx: don't forget to check for the Shift key modifier though...[/quote]
i did...first line in the event handler ;)
[/quote]Doh! Sorry, you are right of course.
-
void MyLineEdit::keyPressEvent(QKeyEvent *event)
keyPressEvent is enough to control all key press event in qt.
-
That works well. Thank you for your help.
PS: What do the function inputMethodEvent actually do? When should I modify it?
-
basically you don't need to use inputMethodEvent handler if you don't know what it is doing ... i know stupid answer ;)
I never had to use it so far. It's basically used if a special QInputContext is installed on the QApplication. So if you use Qt as it is, you don't need to use it.