Important: Please read the Qt Code of Conduct -

[Solved] How to get the KeyPressEvent for . (Dot) key ?

  • Hi...

    I have a QLineEdit in that if I enter a . (Dot), That . (Dot Value) should be changed to " (Quote symbol) in the Line Edit.

    I Tried using the Qt::Key_Period for getting the . (Dot value). But its not working for me. Plz suggest me how to change the LineEdit Value from .(Dot) to "(Quote) .

    Thanks & Regards.

  • I think you should work with keyPressEvent

    See "doc": for more information.

  • Thanks for your suggestion. I know that i have to use this in *keyPressEvent(QKeyEvent e) slot. Inside that slot only i tried using this Qt::key_Period. But i couldn't get the solution.

    Thanks & Regards

  • See the "tetrix-example":

    search for "void TetrixBoard::keyPressEvent(QKeyEvent *event)".

    I fthink your solution should look like this ( QLineEdit allso inherits QWidgets ).

  • in event-Pointer you can call key() returning the pressed key.

  • @
    '.' == QKeyEvent::key()

    don't work ?

  • Nope... It doesn't work.. :-(

    Thanks & Regards

  • One more approach can be to override the eventFilter() function of the lineEdit like :-

    @bool eventFilter(QObject *target, QEvent *event);@

    and in the implementation you can try

    @bool MyLineEdit::eventFilter(QObject *target, QEvent *event)
    if (event->type() == QEvent::KeyPress)
    QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event);
    if (keyEvent->key() == Qt::Key_Period)
    return QWidget::eventFilter(target, event)
    return QWidget::eventFilter(target, event);

    Note : The above approach can be used if you are creating your own custom lineEdit, the keypressevent() will also work incase of custom lineEdit, If you are not using a custom lineEdit then you need to use installEventFilter() on you widget and use the above approach/function but you need to check if target is equal to the lineEdit instance.

    Written brain to terminal not tested

  • Cool.... That Works Fine.... Thanks a lot Sam..... :-)

    Thanks & Regards

Log in to reply