Use of installeventfilter () function

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.