How to process events in Qt

Source: Internet
Author: User
Tags qt designer
I. Event Processing Method in Qt 1. event processing mode 1

First, the event source generates events, and finally the event processor processes these events. However, you may ask,

How can we easily process so many events in Qt? Imagine that if each event corresponds to the same event processor and different events are classified in the event processor, there are two drawbacks: first, as a result, the event processor is too bloated and complicated. Second, this is not easy to expand. When the system adds event types or we need to use custom events, you have to modify the source code of Qt for the purpose. Therefore, the Qt designer provides different event processors for different types of events. Here is another question: How does Qt associate different types of events with the corresponding event processor? It is not hard to guess that there must be a bridge between the event and the event processor. This bridge is the QObject: event () function, which is a virtual function. The sub-class of QObject, such as QWidget, implements this function. The main function of this function is to distribute events, that is, to associate different types of events with the corresponding event processor. This function does not process events, real event processing is performed in the event processor.

For example, when a QWidget generates a QPaintEvent event, the QWidget event function will distribute the event to the QWidget: paintEvent () event processor so that the event can be processed.

The above content is shown in a figure:

2. event processing mode 2

Most of the time, we only care about some specific events, such as mouse clicks or keyboard presses. We do not care about whether other events occur or not, and do not need to process them. The most direct idea at this time is to filter out these events, so that we can avoid handling them, you can also avoid their impact on other parts of the program. Therefore, the event filter concept is introduced in processing mode 2.

If the event filter is installed on the object, the event is intercepted by the Event Filter before it reaches the target object, and processed before being handed over to the target object. The pattern is summarized as follows:


Note: You need to treat it differently here. If you use the installEventFilter () function to register an Event Filter for the target object, this event filter is only valid for this target object, only events of this object need to be passed to the eventFilter () function for filtering, and then the corresponding event processor is called for processing. non-target objects are not affected. If you register an Event Filter for the unique QApplication object in the program, this filter is valid for every object in the program. Any object event is first transmitted to the eventFilter () function, then use the event processor for processing.
3. event processing mode 3

Qt calls QApplicaton to send an event. Therefore, we can re-implement the notify () function in QApplication to obtain and process events, and use this function to obtain events before the Event Filter gets events. However, it is easier to use the Event Filter because we can have multiple event filters, but only one notify () function can be used.
To sum up the mode with a graph:

Ii. methods for handling events in Qt

We can see from the above three processing modes that this is a process of continuous improvement. from the discussion of the three modes, it is not difficult to find a place for event processing, these are the places where we can control event processing during programming.
1. event () function
The first is the event () function that controls event distribution. We can rewrite this function to change the event distribution mode so that the event processing result can be changed.
2. notify () function
This function can intercept events and process the events. However, this method is rarely used and is not described here.
3. Event Filter
The method and result of event processing can be changed by implementing your own Event Filter. This method is commonly used.
4. Event Processor
The last and most important step of event processing is the event processor, because it is the real place for event processing. We can rewrite it with some event processors, to change the processing methods and results of existing events, we can also define our own event types and corresponding event processors.

Note: The last two are most commonly used in the above four methods: Event Filter and event processor.


Additional content:

1. Event Transmission

Many events, including mouse and keyboard events, can be passed. If the event is not intercepted or processed before it reaches the target object, or the target object has been passed to it but is not processed, the parent object of the target object will become a new target object, and the entire event processing process will be repeated until the event is processed or reaches the top-level object.

2. event instance Parsing

The following code is QWidget: The Code of the event () function. From the code, we can see that the event () function only distributes events and is not responsible for event processing. Because there are too many function codes and they are all of the same type, only part of the code is pasted here:

bool QWidget::event(QEvent *event){    Q_D(QWidget);    // ignore mouse events when disabled    if (!isEnabled()) {        switch(event->type()) {        case QEvent::TabletPress:        case QEvent::TabletRelease:        case QEvent::TabletMove:        case QEvent::MouseButtonPress:        case QEvent::MouseButtonRelease:        case QEvent::MouseButtonDblClick:        case QEvent::MouseMove:        case QEvent::TouchBegin:        case QEvent::TouchUpdate:        case QEvent::TouchEnd:        case QEvent::ContextMenu:#ifndef QT_NO_WHEELEVENT        case QEvent::Wheel:#endif            return false;        default:            break;        }    }    switch (event->type()) {    case QEvent::MouseMove:        mouseMoveEvent((QMouseEvent*)event);        break;    case QEvent::MouseButtonPress:        // Don't reset input context here. Whether reset or not is        // a responsibility of input method. reset() will be        // called by mouseHandler() of input method if necessary        // via mousePressEvent() of text widgets.#if 0        resetInputContext();#endif        mousePressEvent((QMouseEvent*)event);        break;    case QEvent::MouseButtonRelease:        mouseReleaseEvent((QMouseEvent*)event);        break;    case QEvent::MouseButtonDblClick:        mouseDoubleClickEvent((QMouseEvent*)event);        break;#ifndef QT_NO_WHEELEVENT    case QEvent::Wheel:        wheelEvent((QWheelEvent*)event);        break;#endif#ifndef QT_NO_TABLETEVENT    case QEvent::TabletMove:    case QEvent::TabletPress:    case QEvent::TabletRelease:        tabletEvent((QTabletEvent*)event);        break;#endif

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.