Void qobject: installeventfilter (qobject *
Filterobj)
Installan Event FilterFilterobjOn this object. For example:
monitoredObj->installEventFilter(filterObj);
An event filter is an object that has es all events that are sent to this object. The filter can either stop the event or forward it to this object. The Event FilterFilterobjReceives events via its eventfilter ()
Function. The eventfilter () function must return true if the event shocould be filtered, (I. e. Stopped); otherwise it must return false.
If multiple event filters are installed on a single object, the filter that was installed last is activated first.
Here'sKeypresseaterClass that eats the key presses of its monitored objects:
class KeyPressEater : public QObject { Q_OBJECT ... protected: bool eventFilter(QObject *obj, QEvent *event); }; bool KeyPressEater::eventFilter(QObject *obj, QEvent *event) { if (event->type() == QEvent::KeyPress) { QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event); qDebug("Ate key press %d", keyEvent->key()); return true; } else { // standard event processing return QObject::eventFilter(obj, event); } }
And here's how to install it on two widgets:
KeyPressEater *keyPressEater = new KeyPressEater(this); QPushButton *pushButton = new QPushButton(this); QListView *listView = new QListView(this); pushButton->installEventFilter(keyPressEater); listView->installEventFilter(keyPressEater);