Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. keyPressEvent not handling backspace

keyPressEvent not handling backspace

Scheduled Pinned Locked Moved Solved General and Desktop
6 Posts 3 Posters 10.0k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    ChajusSaib
    wrote on last edited by
    #1

    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);
        }
    }
    
    1 Reply Last reply
    0
    • R Offline
      R Offline
      Rondog
      wrote on last edited by Rondog
      #2

      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.

      C 1 Reply Last reply
      1
      • R Rondog

        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.

        C Offline
        C Offline
        ChajusSaib
        wrote on last edited by ChajusSaib
        #3

        @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();
        
        1 Reply Last reply
        0
        • R Offline
          R Offline
          Rondog
          wrote on last edited by
          #4

          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.

          1 Reply Last reply
          1
          • raven-worxR Offline
            raven-worxR Offline
            raven-worx
            Moderators
            wrote on last edited by
            #5

            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.

            --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
            If you have a question please use the forum so others can benefit from the solution in the future

            1 Reply Last reply
            1
            • C Offline
              C Offline
              ChajusSaib
              wrote on last edited by
              #6

              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);
              }
              
              1 Reply Last reply
              0

              • Login

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved