When QT creates a Qevent event object, it invokes the Qobject event () function to distribute the events. Sometimes you may need to do something else before calling the event () function, for example, some components on the dialog may not need to respond to the event of a carriage return, at which point you will need to redefine the component event () function. If you have a lot of components, you need to rewrite the event () function more than once, which is obviously inefficient. To do this, you can use an event filter to determine whether the event () function needs to be invoked.
Qojbect has a eventfilter () function that is used to create an event filter. The signature of this function is as follows:
virtual BOOL Qobject::eventfilter (QObject * watched, qevent * event)
If the watched object has an event filter installed, the function is invoked and the event is filtered before it is wheeled to the component for event handling. When overriding this function, you need to return true if you need to filter out an event, such as to stop responding to this event.
bool MainWindow::eventFilter(QObject *obj, QEvent *event)
{
if (obj == textEdit) {
if (event->type() == QEvent::KeyPress) {
QKeyEvent *keyEvent = static_cast<QKeyEvent*>(event);
qDebug() << "Ate key press" << keyEvent->key();
return true;
} else {
return false;
}
} else {
// pass the event on to the parent class
return QMainWindow::eventFilter(obj, event);
}
}
The example above establishes an event filter for MainWindow. To filter events on a component, you first need to determine which component the object is, and then determine the type of event. For example, I do not want the TextEdit component to handle keyboard events, so I first find this component, if this event is a keyboard event, return True directly, that is, filter out this event, other events will continue processing, so return false. For other components, we do not guarantee that there is a filter, so the most safe way is to call the parent class function.
After creating the filter, the next thing to do is to install the filter. The Installeventfilter () function is called to install the filter. The signature of this function is as follows:
void Qobject::installeventfilter (QObject * filterobj)
This function is a function of qobject, so it can be installed into any qobject subclass, not just the UI component. This function receives a Qobject object, and the component that calls this function to install the event filter invokes the Filterobj defined EventFilter () function. For example, Textfield.installeventfilter (obj), if an event is sent to the TextField component, the Obj->eventfilter () function is invoked before textfield.event () is invoked.
Of course, you can also install the event filter on top of the qapplication so that you can filter all the events and gain greater control over them. However, the consequence of this is that it reduces the efficiency of the distribution of events.
If a component has more than one filter installed, the last one will be invoked first, similar to the stack behavior.
Note that if you delete a receive component in the event filter, be sure to set the return value to true. Otherwise, QT will still distribute the event to the receiving component, causing the program to crash.
Event filters and installed components must be on the same thread, otherwise the filter will not work. In addition, if the two components come to a different thread after install, then the filter will not work until they return to the same thread.
The call to the event will eventually invoke the Qcoreapplication notify () function, so the maximum control is actually the Notify () function that overrides the qcoreapplication. As you can see, QT event handling is actually layered five levels: Redefine the event handler function, redefine the events () function, install event filters for a single component, install event filters for Qapplication, and redefine qcoreapplication notify () Function. These levels of control are increasing by layer.
Source: http://devbean.blog.51cto.com/448512/231861