After QT creates a qevent event object, it calls the qobject event () function for event distribution. Sometimes, you may need to do some other operations before calling the event () function. For example, some components in the dialog box may not need to respond to the event of press Enter. At this time, you need to redefine the event () function of the component. If there are many components, You need to rewrite the event () function many times, which is obviously inefficient. Therefore, you can use an Event Filter to determine whether to call the event () function.
Qojbect has an eventfilter () function used to create an Event Filter. The signature of this function is as follows:
Virtual Bool Qobject: eventfilter (qobject * watched, qevent * Event )
If the event filter is installed on the watched object, this function will be called and used for Event Filtering before the component can process the event. When rewriting this function, if you need to filter out an event, such as stopping the response to this event, you need to return true.
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 );
}
}
In the above example, an event filter is created for mainwindow. To filter events on a component, you must first determine which component the object is and then determine the type of the event. For example, if I don't want the textedit component to handle keyboard events, I first find this component. If this event is a keyboard event, true is returned directly, that is, the event is filtered out, other events still need to be processed, so false is returned. For other components, we do not guarantee whether there is a filter, so the safest way is to call the function of the parent class.
After a filter is created, install the filter. To install the filter, you must call the installeventfilter () function. 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 to any qobject subclass, not just the UI component. This function receives a qobject object. components that call this function to install the Event Filter will call the eventfilter () function defined by filterobj. For example, in textfield. installeventfilter (OBJ), if an event is sent to the textfield component, the obj-> eventfilter () function is called before textfield. Event () is called ().
Of course, you can also install the Event Filter on qapplication to filter all events and gain greater control. However, the consequence of doing so is to reduce the efficiency of event distribution.
If a component is installed with multiple filters, the last installed filter is called first, similar to the stack action.
Note: If you delete a receiving component in the Event Filter, you must set the return value to true. Otherwise, QT still delivers the event to the receiving component, resulting inProgramCrash.
The Event Filter and installed components must be in the same thread. Otherwise, the filter does not work. In addition, if the two components arrive at different threads after the install, the filter will be valid only when the two return to the same thread.
events call the notify () function of qcoreapplication. Therefore, the maximum control is actually to override the notify () function of qcoreapplication. It can be seen that QT event processing is actually divided into five layers: redefinition of event processing functions, redefinition of event () functions, and installation of event filters for a single component, install the Event Filter for qapplication and redefine the notify () function of qcoreapplication. The control of these layers is increased layer by layer.