Details
-
Suggestion
-
Resolution: Unresolved
-
Not Evaluated
-
None
-
None
-
None
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
- is duplicated by
-
QTBUG-29731 Generic event filter creation.
- Reported