Logo Search packages:      
Sourcecode: qt4-x11 version File versions

void QObject::installEventFilter ( QObject filterObj  )  [inherited]

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.
See also:
removeEventFilter(), eventFilter(), event()

Definition at line 1941 of file qobject.cpp.

Referenced by Q3ScrollView::addChild(), Q3MainWindow::addDockWindow(), QDataWidgetMapper::addMapping(), QtWindowListMenu::addWindow(), QAbstractItemView::commitData(), QScrollArea::event(), Q3FileDialog::init(), Q3Table::init(), Q3MainWindow::menuBar(), Q3DockArea::moveDockWindow(), QMotifStyle::polish(), QPlastiqueStyle::polish(), QWindowsStyle::polish(), Q3ComboBox::popup(), Q3DockArea::Q3DockArea(), Q3MainWindow::Q3MainWindow(), Q3WhatsThis::Q3WhatsThis(), QtWindowListMenu::QtWindowListMenu(), QX11EmbedContainer::QX11EmbedContainer(), QX11EmbedWidget::QX11EmbedWidget(), Q3Table::setCellWidget(), Q3MainWindow::setCentralWidget(), QMenuBar::setCornerWidget(), Q3ComboBox::setLineEdit(), Q3MainWindow::setMenuBar(), QCompleter::setPopup(), QScrollArea::setWidget(), QFocusFrame::setWidget(), and Q3ListViewItem::startRename().

{
    Q_D(QObject);
    if (!obj)
        return;

    QWriteLocker locker(QObjectPrivate::readWriteLock());

    // clean up unused items in the list
    d->eventFilters.removeAll((QObject*)0);
    d->eventFilters.removeAll(obj);
    d->eventFilters.prepend(obj);
}


Generated by  Doxygen 1.6.0   Back to index