Qt event filter

Event filter

1. The event filter function provided in the event model of Qt enables one QObject object to monitor events in another QObject object. (for example, let QDialog monitor the mouse events of its child controls)

2. By installing event filters in a QObject object, events can be captured before they arrive at the object, so as to monitor the event of the object.


If I have a control Dialog, which is composed of three qlabels, the label is for displaying pictures. If we want to click one of the labels, the label will be enlarged. We can rewrite the keyPressEvent() of these labels, but it's a bit troublesome... We can have QDialog monitor mouse events for its child controls..

The event sent to the label control first reaches the eventFilter() function, and then reaches its final destination..

The steps to implement an event filter are as follows:

1. Install the event filter installEventFilter() on the target object to register the object to be managed;

2. In the eventfiler() function of the monitor object, the events of the target object are processed.

    label1->installEventFilter(this);
    label2->installEventFilter(this);
    label3->installEventFilter(this);
bool Dialog::eventFilter(QObject *watched, QEvent *event)
{
    if(watched==label1)
    {
        if(event->type()==QEvent::MouseButtonPress)
        {
            QMouseEvent *mouseEvent=(QMouseEvent *)event;
            if(mouseEvent->buttons()&Qt::LeftButton)
            {
                stateLabel->setText(tr("Left click to press the left picture"));
            }
            else if(mouseEvent->buttons()&Qt::MidButton)
            {
                stateLabel->setText(tr("Middle press the left picture"));
            }
            else if(mouseEvent->buttons()&Qt::RightButton)
            {
                stateLabel->setText(tr("Right click the left picture"));
            }

            QMatrix matrix;
            matrix.scale(1.8,1.8);
            QImage tmpImg=Image1.transformed(matrix);
            label1->setPixmap(QPixmap::fromImage(tmpImg));
        }
        if(event->type()==QEvent::MouseButtonRelease)
        {
            stateLabel->setText(tr("Mouse release left picture"));
            label1->setPixmap(QPixmap::fromImage(Image1));
        }
    }
    else if(watched==label2)
    {
        if(event->type()==QEvent::MouseButtonPress)
        {
            QMouseEvent *mouseEvent=(QMouseEvent *)event;
            if(mouseEvent->buttons()&Qt::LeftButton)
            {
                stateLabel->setText(tr("Left click middle picture"));
            }
            else if(mouseEvent->buttons()&Qt::MidButton)
            {
                stateLabel->setText(tr("Middle press middle picture"));
            }
            else if(mouseEvent->buttons()&Qt::RightButton)
            {
                stateLabel->setText(tr("Right click middle picture"));
            }

            QMatrix matrix;
            matrix.scale(1.8,1.8);
            QImage tmpImg=Image2.transformed(matrix);
            label2->setPixmap(QPixmap::fromImage(tmpImg));
        }
        if(event->type()==QEvent::MouseButtonRelease)
        {
            stateLabel->setText(tr("Mouse release middle picture"));
            label2->setPixmap(QPixmap::fromImage(Image2));
        }
    }
    else if(watched==label3)
    {
        if(event->type()==QEvent::MouseButtonPress)
        {
            QMouseEvent *mouseEvent=(QMouseEvent *)event;
            if(mouseEvent->buttons()&Qt::LeftButton)
            {
                stateLabel->setText(tr("Left click to press the right picture"));
            }
            else if(mouseEvent->buttons()&Qt::MidButton)
            {
                stateLabel->setText(tr("Middle press the right picture"));
            }
            else if(mouseEvent->buttons()&Qt::RightButton)
            {
                stateLabel->setText(tr("Right click to press the right picture"));
            }

            QMatrix matrix;
            matrix.scale(1.8,1.8);
            QImage tmpImg=Image3.transformed(matrix);
            label3->setPixmap(QPixmap::fromImage(tmpImg));
        }
        if(event->type()==QEvent::MouseButtonRelease)
        {
            stateLabel->setText(tr("Mouse release right picture"));
            label3->setPixmap(QPixmap::fromImage(Image3));
        }
    }


http://blog.csdn.net/rl529014/article/details/53446455

Keywords: Qt

Added by blkrt10 on Sat, 02 May 2020 01:51:37 +0300