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

Lambda / template eventfilter support

XMLWordPrintable

    • Icon: Suggestion Suggestion
    • Resolution: Unresolved
    • Icon: P4: Low P4: Low
    • None
    • None
    • Core: Object Model
    • None
    • All

      Wouldn't it be nice if QObject allowed installing a lambda event filter?

       

      bool QObject::installEventFilter(std::function<bool (QEvent *)> filter)
      bool QObject::installEventFilter(QObject *context, std::function<bool (QEvent *)> filter)
      
      // Templated
      template<typename T>
      bool QObject::installEventFilter(std::function<bool (T *)> filter)
      template<typename T>
      bool QObject::installEventFilter(QObject *context, std::function<bool (T *)> filter)
      

      Then if you wanted to install an event filter, you wouldn't need to reimplement a virtual method in an arbitrary class (a method which always looks a malplaced if you ask me):

      button->installEventFilter(this, [](QEvent *event) {
          if (event->type() == QEvent::KeyPress) {
              QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event);
              qDebug("Ate key press %d", keyEvent->key());
              return true;
          }
          // standard event processing
          return false;
      });

      or even better:

      button->installEventFilter<QEvent::KeyPress>(this, [](auto *event) {
          qDebug("Ate key press %d", event->key());
          return true;
      });
      

       

       

       

       

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

            srutledg Shawn Rutledge
            andrhans Andreas Aardal Hanssen
            Votes:
            0 Vote for this issue
            Watchers:
            5 Start watching this issue

              Created:
              Updated:

                There are no open Gerrit changes