event和eventFilter

1. QEvent event interception

The value returned by the QEvent event is true, you can intercept other events, such as mousePressEvent and other events.

All events in Qt are distributed by QEvent.

After the event object is created, Qt passes the event object to QObject's event() function. The event() function does not directly process events, but distributes these event objects to different event handlers according to their different types. 
The event() function is mainly used for event distribution. 
So, if you want to do some operations before the event is distributed, you can rewrite the event() function. For example, if we want to monitor the press of the tab key in a QWidget component, we can inherit QWidget and rewrite its event() function to achieve this goal: 
Write picture description here

Use the QEvent::type() function to check the actual type of the event, and its return value is an enumeration of the QEvent::Type type. After we have processed the event that we are interested in, we can directly return true to indicate that we have processed the event; for other events that we do not care about, we need to call the event() function of the parent class to continue forwarding, otherwise this component will be Can only handle the events we defined.

Qt uses QEvent::type() to determine the type of event, and then calls a specific event handler.


//前面存在事件重写函数
void myLabel::mousePressEvent(QMouseEvent *ev)
{
...
}

/*
*此处用于拦截事件
*/
bool myLabel::event(QEvent *event)
{
if(event->type()==QEvent::MouseButtonPress)
{

...
return true;//表示  鼠标按下  用户自己处理
}
//其它事件  交给父类处理
returun QLabel::event(event);
}

2. eventFilter() 
QObject has an eventFilter() function to create event filters. The signature of this function is as follows:

  virtual bool QObject::eventFilter(QObject* watched, QEvent* event);
  • 1

eventFilter has two parameters, one is the object, and the other is the time, which can be processed by what type of event and what object.

The principle of the event filter: it will check the received events. If this event is of the type we are interested in, we will do our own processing; if it is not, we will continue to forward it. This function returns a bool type. If you want to filter out the parameter event, for example, if you don't want it to continue forwarding, return true, otherwise return false. The call time of the event filter is before the target object (that is, the watched object in the parameter) receives the event object. In other words, if you stop an event in an event filter, the watched object and all subsequent event filters will never know about such an event.

The eventFilter() function is equivalent to creating a filter, and then we need to install this filter. To install the filter, you need to call the QObject::installEventFilter() function. The signature of this function is as follows:

 void QObject::installEventFilter(QObject* filterobj);
  • 1

This function accepts a parameter of type QObject *. Remember what we said earlier, the  eventFilter() function is a member function of QObject, so any QObject can be used as an event filter (the problem is that if you don’t override the eventFilter() function, this event filter has no effect , Because nothing is filtered by default). Existing filters can be removed by the QObject::removeEventFilter() function.

The event filter and the component to which the filter is installed must be in the same thread, otherwise, the filter will not work.

Note that if you delete a receiving component in the event filter, be sure to set the function return value to true. Otherwise, Qt will still distribute the event to this receiving component, causing the program to crash.

Guess you like

Origin blog.csdn.net/weixin_41882459/article/details/111568529