Uploaded image for project: 'Qt'
  1. Qt
  2. QTBUG-32004

QQuickItem: cannot filter key events

    XMLWordPrintable

Details

    • qtdeclarative: 536e1cccf15963b586f3112a0654ddc0d101fa69

    Description

      It is currently impossible to use QObject::eventFilter() to filter events of a QQuickItem. This used to work in Qt 4 with QDeclarativeItem. Run the test case below, hit some keys, and notice that the filter never receives QEvent::KeyPress nor QEvent::KeyRelease.

      #include <QtQuick>
      
      class Filter : public QObject
      {
          bool eventFilter(QObject* object, QEvent* event)
          {
              if (event->type() == QEvent::KeyPress || event->type() == QEvent::KeyRelease || event->type() == QEvent::ShortcutOverride) {
                  // always a shortcut override (double)...?
                  // QQuickItem(0x13d5810, name = "item") QKeyEvent(ShortcutOverride, 41, 0, ""a"", false, 1) 
                  qDebug() << object << static_cast<QKeyEvent*>(event);
              }
              return false;
          }
      };
      
      int main(int argc, char* argv[])
      {
          QGuiApplication app(argc, argv);
          Filter filter;
          QQmlApplicationEngine engine;
          engine.loadData("import QtQuick 2.1; import QtQuick.Window 2.1; Window { width: 360; height: 360; Item { anchors.fill: parent; objectName: 'item'; focus: true } }");
          QWindow* window = qobject_cast<QWindow*>(engine.rootObjects().value(0));
          if (window) {
              window->show();
              QObject* item = window->findChild<QObject*>("item");
              if (item)
                  item->installEventFilter(&filter);
          }
          return app.exec();
      }
      

      QQuickItemPrivate::deliverKeyEvent() should use QCoreApplication::sendEvent() or notify() instead of calling the event handlers directly. Using sendEvent() or notify() is not entirely free of problems, though, because then the key event handlers do no longer get called. So perhaps QQuickItem::event() should actually deliver the key event to keyPress/ReleaseEvent().

      Attachments

        Issue Links

          No reviews matched the request. Check your Options in the drop-down menu of this sections header.

          Activity

            People

              dandrader Daniel d'Andrada
              jpnurmi J-P Nurmi
              Votes:
              4 Vote for this issue
              Watchers:
              5 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved:

                Gerrit Reviews

                  There are no open Gerrit changes