Execute the required behavior in the monitored code. This can be achieved using Event Filter. Setting an Event Filter involves two steps:
1. Call installeventfilter () on the target object to register the monitoring object to the target object.
2. Process events of the target object in the eventfilter () method of the monitoring object.
Registering a monitoring object in the Ctor is a good place:
Customerinfodialog: customerinfodialog (qwidget * parent): qdialog (parent)
{
...
Firstnameedit-> installeventfilter (this );
Lastnameedit-> installeventfilter (this );
Cityedit-> installeventfilter (this );
Phonenumberedit-> installeventfilter (this );
}
Once the Event Filter is registered, events sent to firstnameedit, lastnameedit, cityedit, and phonenumberedit are first sent to the eventfilter () function of customerinfodialog before being sent to the original destination.
This is the eventfilter () function that receives these events:
Bool customerinfodialog: eventfilter (qobject * target, qevent * event)
{
If (target = firstnameedit | target = lastnameedit
| Target = cityedit | target = phonenumberedit)
{
If (Event-> type () = qevent: keypress)
{
Qkeyevent * keyevent = static_cast <qkeyevent *> (event );
If (keyevent-> key () = QT: key_space)
{
Focusnextchild ();
Return true;
}
}
}
Return qdialog: eventfilter (target, event );
}
Qt Event Filter eventfilter