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

Lambda / template eventfilter support

    XMLWordPrintable

Details

    • Suggestion
    • Resolution: Unresolved
    • Not Evaluated
    • None
    • None
    • Core: Object Model
    • None
    • All

    Description

      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;
      });
      

       

       

       

       

      Attachments

        Issue Links

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

          Activity

            People

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

              Dates

                Created:
                Updated:

                Gerrit Reviews

                  There are no open Gerrit changes