UID:101427
UID:78397
void QObject::installEventFilter ( QObject * filterObj )Installs an event filter filterObj on this object. For example:monitoredObj->installEventFilter(filterObj);An event filter is an object that receives all events that are sent to this object. The filter can either stop the event or forward it to this object. The event filter filterObj receives events via its eventFilter() function. The eventFilter() function must return true if the event should 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's a KeyPressEater class 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);The QShortcut class, for example, uses this technique to intercept shortcut key presses.Warning: If you delete the receiver object in your eventFilter() function, be sure to return true. If you return false, Qt sends the event to the deleted object and the program will crash.Note that the filtering object must be in the same thread as this object. If filterObj is in a different thread, this function does nothing. If either filterObj or this object are moved to a different thread after calling this function, the event filter will not be called until both objects have the same thread affinity again (it is not removed).See also removeEventFilter(), eventFilter(), and event().
UID:96523
UID:91474
UID:97396