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. [SOLVED] Detect QKeyEvent in every children of a QWidget
Qt 6.11 is out! See what's new in the release blog

[SOLVED] Detect QKeyEvent in every children of a QWidget

Scheduled Pinned Locked Moved General and Desktop
6 Posts 2 Posters 2.3k Views 2 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.
  • M Offline
    M Offline
    Max13
    wrote on last edited by Max13
    #1

    I'm on a "cash register" app for family business. I have a barcode scanner, touch-screen and a USB numeric keypad.

    In my "cash register" widget, there is few buttons and a QTableView (to show the cart). I need to detect when either "+, -, *, /" keys are pressed, but also any other.

    These 4 keys are "actions" ("plus" will show a list, "minus" will delete the last product, etc...) and any other key (mostly numeric) will show a dialog with a QLineEdit containing any pressed keys until "enter" is pressed (automatically sent by the barcode scanner) to validate a scan or a manual barcode.

    When I've reimplemented MyWidget::keyPressEvent(), it was only detected when the focus was on MyWidget, and the problem is that when you press a button (MyWidget's child) you set the focus on the button, same if you select a line in the QTableView. Is there an efficient way to reimplement keyPressEvent() in the parent widget to listen for all the children's keyPressEvents?

    I tried with QShortcut (as Qt::WindowShortcut) which is fine but I can't detect every other keys. Any idea?

    We all have started by asking questions. Then after some time, we can begin answering them.

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      What about an eventFilter that you put on your widgets ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      1
      • M Offline
        M Offline
        Max13
        wrote on last edited by
        #3

        I'll give a try.
        Actually, I thought I would have the same problem with eventFilter (concerning the focus) or maybe you're telling me I will have to install the same eventFilter on every child of my QWidget?

        We all have started by asking questions. Then after some time, we can begin answering them.

        1 Reply Last reply
        0
        • M Offline
          M Offline
          Max13
          wrote on last edited by
          #4

          Ok... This is strange. The filter successfully detects keyPresses in the widget, no matter which one is focused (between children). But I don't understand why, it only detects modifiers keys :/

          @class CashRegisterEventFilter : public QObject
          {
          protected:
          bool eventFilter(QObject *obj, QEvent *event)
          {
          if (event->type() == QEvent::KeyPress) {
          QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event);
          qDebug() << "Key pressed:" << (Qt::Key) keyEvent->key();
          return (true);
          }

                  return QObject::eventFilter(obj, event);
              }
          
          public:
              CashRegisterEventFilter(QObject *parent) : QObject(parent) {}
          

          };@

          Then:

          @this->m_eventFilter = new CashRegisterEventFilter(this);
          this->installEventFilter(this->m_eventFilter);@

          Debug only shows something when I press "Alt", "Shift", "cmd"... I'm on OS X 10.10.2, any idea?

          We all have started by asking questions. Then after some time, we can begin answering them.

          1 Reply Last reply
          0
          • M Offline
            M Offline
            Max13
            wrote on last edited by
            #5

            NEVERMIND !

            Thank you, it's my view's fault. The view already captures the alphanumerical key presses.

            I'm implementing with eventFilter, it seems to be working as expected.

            We all have started by asking questions. Then after some time, we can begin answering them.

            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

              Out of curiosity, are you using several CashRegisterEventFilter ?

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              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