keyPressEvent not handling backspace



  • For some reason the backspace key doesn't register and click the button while the rest of my key presses work. When I press the comma button it clicks the button but not when I press backspace.

    void Calculator::keyPressEvent(QKeyEvent *event)
    {
        switch (event->key())
        {
           //....
            case Qt::Key_Comma:
            case Qt::Key_Backspace:
                ASMDEPB_Buttons[6]->animateClick();
                break;
    
            default:
                QWidget::keyPressEvent(event);
        }
    }
    


  • It is likely a case of the wrong key. Maybe it is sending Qt::Key_Delete instead? On my Apple keyboard the delete button is in the position and has the same size as the back space key on a typical keyboard.

    The easiest way to find out is to add a line to your program to display the integer value of the key event and then look it up in the Qt::Key enumerated values in the help.

    I have run into this on occasion. For example, the Qt::Key_Enter and Qt::Key_Return are two different keys on a typical keyboard with the numeric keypad on the right side. These are often assumed to do the same thing so you need to handle both unless you want to separate the two for some reason.



  • @Rondog It doesn't seem to be sending the backspace value or delete value at all.
    While values for any other buttons are printed, e.g Escape button, numbers, characters etc.

    qDebug() << event->key();
    


  • There is an event triggered for the backspace (or delete) key. I am certain of that.

    It is possible a child widget that has focus is not propagating this key event to the parent class? If, for example, you have a QLineEdit as a child the backspace key is often used to erase text. If your 'Calculator' (?) application has this as a child widget, with its own key event handler, and it happens to be in focus, this is likely where it is lost and the parent never sees this event from this specific key.


  • Moderators

    whats the base type of Calculator widget class?
    Or is it a custom widget?
    Any event-filters installed?
    Any Child widgets?

    And another hint: when you process a event - like you do in your code snippet - you may also want to call accept() on the event to stop the propagation.



  • Sorry for the late reply, I had to get a few things done with before the holidays. Thank you both! I installed an event-filter because a I had a QLineEdit as a child.

    bool Calculator::eventFilter(QObject *target, QEvent *event)
    {
        if (target == display)
        {
            if (event->type() == QEvent::KeyPress)
            {
                QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event);
                if (keyEvent->key() == Qt::Key_Backspace)
                {
                    ASMDEPB_Buttons[6]->animateClick();
                    return true;
                }
            }
        }
        return QWidget::eventFilter(target, event);
    }
    

Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.