Qt event processor and Event Filter instance

Source: Internet
Author: User

In the previous article, we learned how to process events in Qt, and also mentioned that the most common method is to use the event processor and Event Filter. In this article, let's take a look at how the event processor and Event Filter are used.
I. Event processor instance

In Qt, corresponding event processors are provided for each common event type. If we want to capture a certain type of event and perform custom processing, you only need to rewrite these event processors. For common time types and corresponding event processors, see:

In my program, I used the scroll wheel event, and the main effect is that everyone is familiar with it: using a control to display images, you can adjust the image size when you scroll the scroll wheel.
Here, I want to capture the scroll wheel event of the image display control, and then rewrite the scroll wheel event processor of the control.
First, let's take a look at the mouse wheel event and the corresponding event processor. You can see the figure above:

In The Qt help document, we can see The following statement "The event handler QWidget: wheelEvent () events es wheel events .". In other words, the event processor will call the wheelEvent () function to process the event. Therefore, we need to rewrite the function.

Step 1: declare the function in the header file:

The second step is to implement the wheelEvent () function (Here we mainly focus on the event processing framework, rather than the specific example, so we will not explain the code details too much ):

void ImageWidget::wheelEvent(QWheelEvent *event){    int numDegrees = event->delta();    int num = numDegrees / 120;    if(num < 0)    {        num = 0 - num;    }    if (event->orientation() == Qt::Horizontal) {        //scrollHorizontally(numSteps);        scale *= 1.25;        resize(this->scale * this->size());    } else {        //scrollVertically(numSteps);        if(numDegrees > 0)        {            //scale *= 0.75;            resize(num * 0.75 * this->size());        }        else if(numDegrees < 0)        {            //scale *= 1.25;            resize(num * 1.25 * this->size());        }    }    event->accept();}

This function is implemented here. If you look at the header file above, you will know that other types of event processing are implemented here. In fact, they all share the same idea and find the type of event to be processed, find the corresponding event processor and rewrite the event processing method in the event processor.
Ii. Use the event filter instance

One of the most powerful functions of the Qt event model is that a QObject instance can monitor events in another QObject instance by installing the Event Filter in the target object. Here we proceed with the above example and add a function for the image browser above: I set an automatic playback button in my program and click the button, A series of images are displayed in the image display part, just like playing many images in an animation. When you click the left mouse button on the top, the animation is stopped, but the current picture is stuck; when I right-click the mouse, the animation playback mode is restored.
First, let's talk about the main page layout: I placed a custom image display part in a QMainWindow. As described above, we can easily know that the method to implement this function is to register an Event Filter on the target widget (custom image display widget, at this time, the event filter is what we call the monitoring object. After completing these steps, when an event is generated in the target part, it is first passed to the monitoring object (Event Filter) processing, not the event processor corresponding to the event. Therefore, we can intercept events for processing. After the Monitored object intercepts the event of the target object, it calls its eventFilter () function to process the event.
To sum up, there are two steps:
1. Call installEventFilter () on the target object to register the monitoring object (Event Filter );
Second, rewrite the eventFilter () function of the monitoring object to process events of the target object.
Follow these two steps:
First, use the installEventFilter () function to register the event listener for the target object:

// Register the Event Filter imageWidget-> installEventFilter (this );

Then rewrite the monitor object eventFilter () function:

// Processing bool PMainWindow: eventFilter (QObject * target, QEvent * event) {if (target = imageWidget) {qDebug ("The imageWidget generate event! "); If (event-> type () = QEvent: MouseButtonPress) {QMouseEvent * mouseEvent = static_cast <QMouseEvent *> (event); if (mouseEvent-> buttons () & Qt: LeftButton) {qDebug ("The Left Button Event! "); KillTimer (timeId);} else if (mouseEvent-> buttons () & Qt: RightButton) {qDebug (" The Right Button Event! "); // ClickNum ++; // again (); timeId = startTimer (3000);} return true ;}} // events generated by other components are handed over to the base class for handling return QMainWindow: eventFilter (target, event );}

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.