Today we're talking about the event () function. Remember that this function has been mentioned before, that QT passes this event object to the Qobject event () function after it has been created. The event () function does not directly handle events, but instead distributes them to different event handlers according to their different types. \ handler
The event () function is used primarily for distribution of events, so if you want to do something before the event is distributed, you need to pay attention to this event () function. To achieve this, we can rewrite the event () function. For example, if you want to move the focus to the next component when the TAB key is pressed in the window, rather than having the focus component handle it, you can inherit Qwidget and rewrite its event () function, which has been achieved:
bool MyWidget::event(QEvent *event) {
if (event->type() == QEvent::KeyPress) {
QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event);
if (keyEvent->key() == Qt::Key_Tab) {
// 处理Tab鍵
return true;
}
}
return QWidget::event(event);
}
The event () function accepts a Qevent object, which is the object that requires this function to be forwarded. In order to forward, there must be a series of type judgments, which can call the Qevent type () function whose return value is an enumeration of qevent::type types. Once we have dealt with the events we need, we can return directly to the other events we do not care about, we need to invoke the parent class event () function to continue forwarding, otherwise this component can only handle our defined events.
The event () function return value is a bool type, and False if the incoming event is recognized and processed, returns True. If the return value is True,qapplication the event is considered to have been processed, the next event in the event queue will continue to be processed, and if the return value is false,qapplication will try to find the next handler for the event.
The return value of the event () function differs from the accept () and ignore () functions of the events. The Accept () and ignore () functions are used for communication between different event handlers, such as determining whether the event is handled, and the event () function returns a value that notifies the Qapplication whether the Notify () function handles the next event. To make this clearer, let's look at how the Qwidget event () function is defined:
bool QWidget::event(QEvent *event) {
switch (e->type()) {
case QEvent::KeyPress:
keyPressEvent((QKeyEvent *)event);
if (!((QKeyEvent *)event)->isAccepted())
return false;
break;
case QEvent::KeyRelease:
keyReleaseEvent((QKeyEvent *)event);
if (!((QKeyEvent *)event)->isAccepted())
return false;
break;
// more...
}
return true;
}
The Qwidget event () function uses a huge switch to determine the type of the qevent and distributes it to different event handlers. After the event handler function, use the isaccepted () method of this event to know if the event is accepted, and if it is not accepted, the event () function returns false immediately, otherwise it returns true.
Another scenario that must rewrite the event () function is when there are custom events. If you have custom events in your program, you must rewrite the event () function to distribute the custom event, or your custom event will never be invoked. For custom events, we will describe them in a later section.
Source: http://devbean.blog.51cto.com/448512/230883